I'm new to Javascript. When I did some objective oriented programming, I want to invoke constructor of super class in a sub-class:
function P(p1, p2) {
alert(this);
this.v1 = p1;
this.v2 = p2;
}
var middleObj = function() {};
middleObj.prototype = P.prototype;
var C = function(p1, p2) {
P.bind(this, p1, p2);
P();
}
C.prototype = new middleObj();
C.prototype.constructor = C;
var objChild = new C(1,2);
alert(objChild instanceof P);
alert(objChild.v2);
If I call P() directly in C(), this turns to window
, and I can understand that. However, even if I bind this to C(), still I can not get the proper value of this
. As a result, the second alert will show undefined because v2 is not initialized properly.