I know its possible to set a key value with a preceding key value in Javascript for example
var obj = {
one: "yes",
two: obj.one
}
obj[two] is now equal to "yes"
How do i go about setting the value when the keys are in a function
var obj = {
one: function () {
return(
two: "yes"
three: ?? //I want to set three to the value of two
)
}
}
I want to have three contain the value of two i.e obj.one() should return {two: "yes", three: "yes"}
Your first code doesn't work neither. It throws
TypeError: obj is undefined
.You can use
For the second one, you can use
Note each call of
one
will produce a new copy of the object. If you want to reuse the previous one,Try this