Javascript function using "this =" gives "Invalid left side in destination"

I am trying to get a JavaScript object to use the "this" constructor assignments of other objects, and also assume all prototypes of these objects. Here is an example of what I'm trying 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 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 would be grateful for your thoughts on how ObjY injects ObjX assignments into 'this' (i.e. you do not need to repeat all assignments this.$* = *in the ObjY constructor) and ObjY assumes ObjX.prototype.

My first thought is to try the following:

function ObjY(a,b,c) {
   this.prototype = new ObjX(a,b*12);
}

Ideally, I would like to learn how to do this in a prototype way (that is, not to use any of these β€œclassic” OOP substitutes, such as Base2 ).

, ObjY (, factory['ObjX'] = function(a,b,c) { this = ObjX(a,b*12); ... }) - .

.

+5
1

, this , , .

call apply ObjX this ObjY:

function ObjY(a,b,c) {
  ObjX.call(this, a, b * 12); 
  this.$c = c;
}

ObjX this, , , , this ObjY.

call , this , , $c.

:. , prototype , , , .

, prototype [[Prototype]], .

[[Prototype]] new ( [[Construct]]), ( , Mozilla, obj.__proto__;, ECMAScript 5, Object.getPrototypeOf, ).

, , [[Prototype]] , this, prototype.

, @Anurag, ObjY.prototype ObjX:

function ObjY(a,b,c) {
  ObjX.call(this, a, b * 12); 
  this.$c = c;
}

ObjY.prototype = new ObjX();
ObjY.prototype.constructor = ObjY;

, ObjY , ObjX.prototype, , , ObjY.prototype.constructor, , ObjX.

:

+15

All Articles