How can I access the parent attribute of a child object like this?
var foo = function foo(input){ this.input = input; };
function bar(input){ return new foo(input); }
foo.prototype = {
baz : {
qux : function(){
alert(this.parent.input );
}
},
corge : function(){
alert(this.input );
}
}
bar('test').corge(); //alerts 'test'
bar('test').baz.qux(); //errors 'this.parent is undefined'
You can’t.
There is one
bazregardless of how manynew foothere are, so there is no way to map fromthiswhich typically points to the singletonfoo.prototype.bazto a specific instance offoo.It looks like you probably meant to create a
bazper instance offoo.Try this