trouble passing parameters of a subclass constructor through to it's superclass constructor

Discussion in 'Javascript' started by ingoweiss, May 11, 2006.

  1. ingoweiss

    ingoweiss Guest

    Hi,

    I am having trouble passing parameters of a Javascript subclass
    constructor through to it's superclass constructor.

    I am trying all sorts of things, including the below, but nothing
    worked so far.

    Thanks for any help!




    function Base(a)
    {
    this.a = a;
    }

    function Derived(a)
    {
    this.prototype.constructor.call(a);
    }

    Derived.prototype = new Base();
    Derived.prototype.constructor = Derived;

    var derived = new Derived("hello");
    //a should be "hello" now, but it is undefined:
    window.alert(derived.a)
     
    ingoweiss, May 11, 2006
    #1
    1. Advertising

  2. ingoweiss

    VK Guest

    ingoweiss wrote:
    > Hi,
    >
    > I am having trouble passing parameters of a Javascript subclass
    > constructor through to it's superclass constructor.
    >
    > I am trying all sorts of things, including the below, but nothing
    > worked so far.
    >
    > Thanks for any help!
    >
    >
    >
    >
    > function Base(a)
    > {
    > this.a = a;
    > }
    >
    > function Derived(a)
    > {
    > this.prototype.constructor.call(a);
    > }
    >
    > Derived.prototype = new Base();
    > Derived.prototype.constructor = Derived;
    >
    > var derived = new Derived("hello");
    > //a should be "hello" now, but it is undefined:
    > window.alert(derived.a)


    Be careful with an a la class inheritance in the conventional
    JavaScript (thus not JScript.Net and such). Be careful exactly because
    it's an "a la class inheritance", not "the class inheritance".
    Everything can work pretty close to what you may used to in other
    languages, but many features are emulated or missing.
    And for sure you shouldn't use both prototype inheritance and class
    inheritance together. Fists of all it's a needless mess, secondly it's
    error prone, finally (as you just discovered) it simply doesn't work in
    the way you think it should work.

    // by keeping (a la) class inheritance:

    function Base(a) {
    this.foo = 'bar';
    this.a = a;
    }

    function Derived(a,b) {
    Base.call(this,a);
    this.b = b;
    }

    var obj = new Derived('a','b');
    alert(obj.foo);
    alert(obj.a);
    alert(obj.b);
     
    VK, May 11, 2006
    #2
    1. Advertising

  3. ingoweiss wrote:

    [snip]
    > function Derived(a)
    > {
    > this.prototype.constructor.call(a);
    > }

    [/snip]

    [snip]
    > Derived.prototype.constructor = Derived;

    [/snip]

    The last line of code sets the value of the "constructor" property of
    "Derived.prototype" as a reference to "Derived".

    So in your expression "this.prototype.constructor.call", you calling
    Derived, not Base.

    Regards

    Julian Turner
     
    Julian Turner, May 11, 2006
    #3
  4. Julian Turner wrote:
    > ingoweiss wrote:
    > [snip]
    >> function Derived(a)
    >> {
    >> this.prototype.constructor.call(a);
    >> }

    > [/snip]
    >
    > [snip]
    >> Derived.prototype.constructor = Derived;

    > [/snip]
    >
    > The last line of code sets the value of the "constructor"
    > property of "Derived.prototype" as a reference to "Derived".
    >
    > So in your expression "this.prototype.constructor.call",
    > you calling Derived, not Base.


    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, so no function is
    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.

    Richard.
     
    Richard Cornford, May 12, 2006
    #4
  5. 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
     
    Julian Turner, May 12, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pat Ryan
    Replies:
    28
    Views:
    6,602
    Woebegone
    Dec 18, 2003
  2. Matt
    Replies:
    3
    Views:
    15,684
    Tony Morris
    Apr 26, 2004
  3. jstorta
    Replies:
    3
    Views:
    475
    jstorta
    Feb 20, 2006
  4. Evan Klitzke
    Replies:
    0
    Views:
    398
    Evan Klitzke
    Aug 2, 2007
  5. bart van deenen
    Replies:
    6
    Views:
    822
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page