P粉6158297422023-08-30 11:54:25
You can do this:
var foo = { a: 5, b: 6, init: function() { this.c = this.a + this.b; return this; } }.init();
This will be some kind of one-time initialization of the object.
Note that you are actually assigning the return value of init()
to foo
, so you must return that value
.
P粉3223196012023-08-30 00:12:19
Well, the only thing I can tell you is
var foo = {
a: 5,
b: 6,
get c() {
return this.a + this.b;
}
}
console.log(foo.c) // 11