0

Why this type of inheritance will not work in JavaScript. Is there any way to perform this step. Please help

function base() {}
base.prototype.findFirst = function() {
    console.log("FindMe First");
}

function base2() {}
base2.prototype.findMe = function() {
    console.log("FindMe ");
}

function inherit() {
    base.call(this);
    base2.call(this);
}

inherit.prototype = base.prototype;
inherit.prototype.constructor = inherit;
inherit.prototype = base2.prototype;
inherit.prototype.constructor = inherit;

var test = inherit();
test.findFirst();
test.findMe();
11mb
  • 1,339
  • 2
  • 16
  • 33
Siddharth
  • 545
  • 2
  • 7
  • 24
  • 1
    in javascript objects cannot have multiple prototype chain.this is not possible. – Prabhu Murthy Sep 22 '14 at 13:13
  • 3
    I'd suggest you're probably #doingitwrong. JS is a functional programming language, not an object oriented language. You're probably better off using the powerful functional features available (eg. closures) than trying to kludge an OOP implementation in because it won't work like you want – Joe Sep 22 '14 at 13:15
  • Multiple inheritance is rarely a good idea even in languages where it is possible. And in Javascript it isn't. – Matt Burland Sep 22 '14 at 13:18
  • 3
    @Joe JS is prototype-based which is another style of OOP. – bitWorking Sep 22 '14 at 13:21
  • 2
    Well, why do you think it would work in the first place? Do you understand how inheritance in JS works at all? – Bergi Sep 22 '14 at 13:24
  • Maybe the following can help you out understanding the subject a bit more. http://stackoverflow.com/a/16063711/1641941 – HMR Sep 22 '14 at 17:02

3 Answers3

2

You are overwritting prototype with base.prototype and then with base2.prototype. So it stores base2.prtotype i.e. which is assigned second. Now if you create an instance of your inherit class var test = new inherit(); you will see that test has base2.property i.e. it has fimeMe() method in test.property.findMe();. To achieve your goal you should try to extend or refer Mixin Multiple Inheritance

Community
  • 1
  • 1
DimoMohit
  • 735
  • 4
  • 17
1

Mixins can be used in javascript to achieve the same goal you probably want to solve via multiple inheritance at the moment.

0

I've extended the Function prototype which is not the best idea, but to give you an idea of how it works. It's not multi inheritance, but it's an inheritance tree.

Function.prototype.extend = function(child)
{
    var childPrototype = child.prototype;
    child.prototype = Object.create(this.prototype);
    for (var property in childPrototype) {
        if (childPrototype.hasOwnProperty(property)) {
            child.prototype[property] = childPrototype[property];
        }
    }
    child.prototype.constructor = child;
    return child;
};

var base = (function()
{
    var base = function()
    {

    };

    base.prototype.findFirst  = function(x, y)
    {
        console.log("FindMe First");
    };

    return base;

}());

var base2 = base.extend(function()
{
    var base2 = function()
    {

    };

    base2.prototype.findMe = function(x, y)
    {
        console.log("FindMe ");
    };

    return base2;

}());

var inherit = base2.extend(function()
{
    var inherit = function()
    {

    };

    return inherit;

}());

var test = new inherit();
test.findFirst();
test.findMe();
bitWorking
  • 12,485
  • 1
  • 32
  • 38
  • I think you should remove the `childPrototype.hasOwnProperty(property)` test - otherwise multilevel inheritance doesn't work well. – Bergi Sep 22 '14 at 14:05
  • Since OP doesn't seem to know what his own code does, would you mind to explain your idea by text instead of code? – Bergi Sep 22 '14 at 14:06
  • @Bergi look [here](http://brianflove.com/2013/09/05/javascripts-hasownproperty-method/) `The use of hasOwnProperty() prevents the loop from enumerating over any inherited properties on the object` – bitWorking Sep 22 '14 at 14:43
  • I know what it does, I'm just telling you that we might *not* want it here… – Bergi Sep 22 '14 at 14:55
  • @Bergi I've tested it without and this can lead to unexpected results. For example if you add a property to the Object: `Object.prototype.test = 'Object';` then it's not possible to override it with `base.prototype.test = 'base';`. Please test it yourself.. – bitWorking Sep 22 '14 at 15:02
  • That's just because your `extend` function doesn't allow overriding properties at all. What I mean is that when you do `something.extend(base2.extend(multiChild))` then `base::findFirst` won't be inherited via `base2`. – Bergi Sep 22 '14 at 15:11
  • @Bergi have you really tested it? It works perfectly fine. It's basically the same as following: [Coffeescript-style classes](https://github.com/njoubert/inheritance.js/blob/master/INHERITANCE.md#2-coffeescript-style-classes). – bitWorking Sep 22 '14 at 15:33
  • Here: http://jsfiddle.net/7s17j2ja/1/ - that is what the OP wanted, and doesn't work with your `extend` function. Creating a multi-level inheritance like in your link is trivial, but that's not what he wants. – Bergi Sep 22 '14 at 15:56