Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototypal inheritance: Can you chain Object.create?

I'm new to prototypal inheritance so I'm trying to understand the 'right' way. I thought I could do this:

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

var tbase = {};

tbase.Tdata = function Tdata() {};

tbase.Tdata.prototype.say = function (data) {
    console.log(data);
};

var tData = new tbase.Tdata();

tbase.BicData = Object.create(tData);

tbase.BicData.prototype.say = function (data) {
    console.log("overridden: " + data)
};

tbase.BicData.prototype.shout = function (data, temp) {
    console.log("SHOUT: " + data + ", " + temp)
};

var test = new tbase.BicData();

tData.say("test1"); 
test.say("test2");
test.shout("test3", "hope");

if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

var tbase = {};

tbase.Tdata = function Tdata() {};

tbase.Tdata.prototype.say = function (data) {
    console.log(data);
};

var tData = new tbase.Tdata();

tbase.BicData = Object.create(tData);

tbase.BicData.prototype.say = function (data) {
    console.log("overridden: " + data)
};

tbase.BicData.prototype.shout = function (data, temp) {
    console.log("SHOUT: " + data + ", " + temp)
};

var test = new tbase.BicData();

tData.say("test1"); 
test.say("test2");
test.shout("test3", "hope");

But instead I get "tbase.BicData.prototype is undefined"

In Java speak, what I want is to have Tdata as a boilerplate 'interface', BicData to be an implementation of that that, and then to instantiate objects from it.

Where am I going wrong?

like image 955
robinhowlett Avatar asked Feb 19 '10 16:02

robinhowlett


People also ask

Which of the following is a feature of prototypal inheritance?

Prototypical inheritance allows us to reuse the properties or methods from one JavaScript object to another through a reference pointer function. All JavaScript objects inherit properties and methods from a prototype: Date objects inherit from Date.

How does prototypal inheritance work?

The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another object. Traditionally, in order to get and set the [[Prototype]] of an object, we use Object. getPrototypeOf and Object.

How does prototypal inheritance work and how is it different from classical inheritance?

Classical inheritance is limited to classes inheriting from other classes. However prototypal inheritance includes not only prototypes inheriting from other prototypes but also objects inheriting from prototypes.

What is prototypal chain?

Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.


1 Answers

The problem is that tbase.BicData is an object (tbase.BicData = Object.create(tData);), and the prototype property should be used on constructor functions.

Taking advantage of the Object.create method, I would do something like this:

var tbase = {};

tbase.Tdata = {
  say : function (data) {
    console.log(data);
  }
};

tbase.BicData = Object.create(tbase.Tdata);

tbase.BicData.say = function (data) {
    console.log("overridden: " + data)
};

tbase.BicData.shout = function (data, temp) {
    console.log("SHOUT: " + data + ", " + temp)
};

var test = Object.create(tbase.BicData);
var tData = Object.create(tbase.Tdata);

tData.say("test1"); // test1
test.say("test2"); // overridden: test2
test.shout("test3", "hope"); // SHOUT: test3, hope
like image 90
Christian C. Salvadó Avatar answered Sep 19 '22 04:09

Christian C. Salvadó