I have a class, and have given its prototype several sub-objects to facilitate namespacing. These sub-objects have methods. I can't figure out how to use this
inside those methods to access the properties set with the constructor.
I have a feeling bind
, call
, and apply
are involved somehow, but I'm having a good deal of trouble understanding what those do, and how all this OOP-ness works in general. There are plenty of resources, but they're all either too low-level, or so high-level I don't understand them. Thank you!
function Object(
argument1,
argument2
){
this.property1 = argument1;
this.property2 = argument2;
}
Object.prototype = {
subObject1 : {
method1 : function(){
return this.property1;
}
},
subObject2 : {
method1 : function(){
return this.property2;
}
}
}
var foo = new Object(11, 22);
var bar = new Object(33, 44);
console.log(foo.subObject1.method1()); //I'd like this to return 11
console.log(foo.subObject2.method1()); //I'd like this to return 22
console.log(bar.subObject1.method1()); //I'd like this to return 33
console.log(bar.subObject2.method1()); //I'd like this to return 44