Richard Cornford wrote:
Not really. When - new Derived("hello"); - is executed the - this -
keyword refers to the object being constructed. That object is not a
function. It does not have a - prototype - property,
called because of the run-time error generated when -
this.prototype.constructor - is referenced.
If the function was called it seems that it would be the wrong function,
but passing in the string primitive "hello" to the function's - call -
method would be worthless as a temporary Sting object should be created
for it, passed into the method to act as the - this - object in the
function call, and then be thrown away as no references to it would
remain.
Thank you, yes I forgot those two points. I was too focused on the
fact that the "constructor" property of Derived's prototype was not
even pointing to Base, let alone other problems with the code.
Indeed, if the OP had omitted the "prototype" from the statement, and
properly included a reference to an object in the "call" method thus:-
"this.constructor.call(this,a);"
a stack overflow would have obviously resulted.
The OP may (I am guessing) have been thinking of a pattern along the
lines of the Java "super" keyword, as an alternative to the patten
suggested by VK:-
function Base(a)
{
this.myProp=a;
}
function Derived(a)
{
/* this.superClassConstructor.call(this,a) */
/* or more simply ... */
this.superClassConstructor(a);
}
Derived.prototype=new Base();
Derived.prototype.constructor=Derived;
Derived.prototype.superClassConstructor=Base;
Regards
Julian Turner