So, I want to create an object that can contain functions and variables and objects containing functions and variables, but I find that if there are "top-level" variables, they can't be accessed by deeper functions due to scoping rules. For example:
var myobj = {
foo: "test",
bar: function() {
return this.foo;
},
baz: {
piz: "test2",
poz: function() {
return this.piz + this.foo;
}
}
}
console.log(myobj.bar());
console.log(myobj.baz.poz());
The second call return "test2undefined" for obvious reasons - this.foo refers to baz.foo (which is undefined) and not myobj. How can I reference myobj to allow the variable foo to be available everywhere in this object?
thisrefer to two different objects. Maybe if you explain your real-world issue, we can provide useful solutions.