I am trying to get a JavaScript object to use the “this” assignments of another objects’ constructor, as well as assume all that objects’ prototype functions. Here’s an example of what I’m attempting to accomplish:
/* The base - contains assignments to 'this', and prototype functions
*/
function ObjX(a,b) {
this.$a = a;
this.$b = b;
}
ObjX.prototype.getB() {
return this.$b;
}
function ObjY(a,b,c) {
// here's what I'm thinking should work:
this = ObjX(a, b * 12);
/* and by 'work' I mean ObjY should have the following properties:
* ObjY.$a == a, ObjY.$b == b * 12,
* and ObjY.getB == ObjX.prototype.getB
* ... unfortunately I get the error:
* Uncaught ReferenceError: Invalid left-hand side in assignment
*/
this.$c = c; // just to further distinguish ObjY from ObjX.
}
I’d be grateful for your thoughts on how to have ObjY subsume ObjX’s assignments to ‘this’ (i.e. not have to repeat all the this.$* = * assignments in ObjY’s constructor) and have ObjY assume ObjX.prototype.
My first thought is to try the following:
function ObjY(a,b,c) {
this.prototype = new ObjX(a,b*12);
}
Ideally I’d like to learn how to do this in a prototypal way (i.e. not have to use any of those ‘classic’ OOP substitutes like Base2).
It may be noteworthy that ObjY will be anonymous (e.g. factory['ObjX'] = function(a,b,c) { this = ObjX(a,b*12); ... }) — if I’ve the terminology right.
Thank you.
You can’t really do that, because the
thisvalue by definition is immutable, you can’t change in that way what it references to.A workaround would be to use the
callorapplymethods to run yourObjXconstructor function in thethisobject ofObjY:In the above example, the
ObjXfunction is executed changing itsthisvalue, so all property extensions you make to that object in this function, will be reflected in the new object that thethisvalue refers in theObjYconstructor.As soon the
callmethod ends, thethisobject will be augmented and you can make more property extensions, like adding your$cvalue.Edit: About the prototypes, your sample will not work, because the
prototypeproperty has no special meaning for object in stances it will be just as any other property, it should be used on constructor functions.I think you might be confusing the
prototypeproperty of constructors with the internal[[Prototype]]property that all objects have.The
[[Prototype]]property can only be set by thenewoperator (through the[[Construct]]internal operation), this property can’t be changed, (although some implementations, like the Mozilla one, you can access it throughobj.__proto__;, and in ECMAScript 5, theObject.getPrototypeOfmethod has been introduced, but I wouldn’t recommend you to mess directly with it).Actually, when your constructor function is executed, the internal
[[Prototype]]property of the object that thethisvalue refers, has already been set to its constructor’sprototypeproperty.So, as @Anurag comments, you could set the
ObjY.prototypeto a newly createdObjXobject:That will make that your
ObjYinherit also the properties added to theObjX.prototype, and as you see, I changed theObjY.prototype.constructor, since the assignment in the line above will make this property to wrongly point toObjX.Recommended articles: