I have an object structure/hierarchy like the one below:
var obj = {
dimensions : {
x : 100,
y : 100,
w : 300,
h : 400,
cmp : function () {
return this.x + this.y;
}
},
definition : {
base : {
rect1 : {
// how do I get the value of dimensions.x ??
// or, for that matter, is there a way I could call dimensions.cmp()?
},
rect2 : {
// things go here
}
}
}
};
My question is: Is to possible to get the value of dimensions.x from within the dimensions.definition.rect1 function?
You can’t really do that dynamically, the
thisvalue inside a function will point to the object there the function is bound as a property.For example, if you invoke
foo.bar.func();, thethisvalue insidefuncwill refer to thefoo.barobject, there is no way to determine dynamically ifthisbelongs tofoo, without explicitly checking all properties offoo(and of course knowing beforehand where to check).Remember that objects are handled as references, therefore an object can be referenced from many places.
Don’t complicate yourself, find your members using the
objreference, keep it simple: