7

I'm trying to implement the inheritance with the module pattern in this way:

Parent = function () {

    //constructor
    (function construct () {
        console.log("Parent");
    })();

    // public functions
    return this.prototype = {

        test: function () {
            console.log("test parent");
        },


        test2: function () {
            console.log("test2 parent");
        }

    };
};


Child = function () {

    // constructor
    (function () {
        console.log("Child");
        Parent.call(this, arguments);
        this.prototype = Object.create(Parent.prototype);
    })();


    // public functions
    return this.prototype = {

        test: function()
        {
            console.log("test Child");
        }

    }

};

but I can't to call from child's instance the test2().

var c = new Child();
c.test2(); // c.test2 is not a function

What I wrong?

Webman
  • 1,534
  • 3
  • 26
  • 48
  • 2
    [How to implement inheritance in JS Revealing prototype pattern?](http://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern) explains in-depth how to use the module pattern and inheritance. – Bergi Mar 15 '13 at 15:59

2 Answers2

11

You're not using the module pattern in the correct way. Somehow, your "constructor" is called as an immediately-invoked function expression (IIFE), and the module closure is not. It should be the other way round.

Also, you can't assign to this.prototype. To create the prototype object from which all instances will inherit, you will need to assign to the prototype property of the constructor function (the this keyword even pointed to the global window object in your case).

And you should return the constructor function, not the prototype object, from the IIFE as soon as you have it.

Parent = (function () {
    // constructor
    function construct () {
        console.log("Parent");
    };

    // public functions
    construct.prototype.test = function () {
        console.log("test parent");
    };
    construct.prototype.test2 = function () {
        console.log("test2 parent");
    };

    return construct;
})();


Child = (function () {
    // constructor
    function construct() {
        console.log("Child");
        Parent.apply(this, arguments);
    }

    // make the prototype object inherit from the Parent's one
    construct.prototype = Object.create(Parent.prototype);
    // public functions
    construct.prototype.test = function() {
        console.log("test Child");
    };

    return construct;
})();
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Thanks so much. Now I would have another question: Is it possibile to add in Child a static function? – Webman Mar 15 '13 at 16:25
  • 2
    Depends on what you mean by "static", there's no real equivalent to that in the dynamic JS language :-) However, you could assign a function to the `Child` constructor function object: `Child.method = function(){…};` (or inside the module closure, `construct.method = …;`) – Bergi Mar 15 '13 at 16:28
  • 1
    with "static" I mean call a function without to create an istance of `Child`. Something like this `Child.myStaticFunction()` – Webman Mar 15 '13 at 16:32
  • 1
    It's not clear why this line :`Parent.apply(this, arguments);` is there. it does nothing in the relevant code. – Royi Namir Aug 28 '15 at 12:31
  • 1
    @RoyiNamir: It does execute the parent constructor, as visible from the side effect `console.log("Parent");`. Of course, it actually does nothing, but it's there in case the parent will get extended in the future. Just a good practise to keep the code maintainable, and it does no harm :-) – Bergi Aug 28 '15 at 14:22
0
(function () {
    console.log("Child");
    Parent.call(this, arguments);
    this.prototype = Object.create(Parent.prototype);
})();

this refers to window, because you wrapped your code into a function. Remove the wrapping function or pass this as argument.

a better oliver
  • 26,330
  • 2
  • 58
  • 66