This question already has an answer here:
say I have
myfunc({
var1: 1,
var2: 2,
})
if i want to have a value that makes use of the current unnamed object, how would I do this?
eg if I wanted
myfunc({
var1: 1,
var2: 2,
var3: this.var1 + this.var2
})
obviously this does not work.
What would the correct syntax be?
You can't do it in the notation you're using. An object literal doesn't have any notion of itself in this context.
You could make var3 a function, and calling it will allow you to use 'this':
Unfortunately, that isn't possible. While an object literal is being constructed, no external reference to that object exists until the entire literal is evaluated. The only way to use
this
at this stage is to use a constructor instead: