Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use 'prototype' for javascript inheritance?

JavaScript objects have the 'prototype' member to facilitate inheritance. But it seems, we can live perfectly well, even without it, and I wondered, what are the benefits of using it. I wondered what are the pros and cons.

For example, consider the following (here jsfiddle):

function Base (name) {

    this.name = name;
    this.modules = [];
    return this;
}

Base.prototype =
{
    initModule: function() {
        // init on all the modules.
        for (var i = 0; i < this.modules.length; i++)
            this.modules[i].initModule();
        console.log("base initModule");
    }   
};

function Derived(name) {
       Base.call(this,name); // call base constructor with Derived context
}

Derived.prototype = Object.create(Base.prototype);

Derived.prototype.initModule = function () {
      console.log("d init module");
      //  calling base class functionality
      Base.prototype.initModule.call(this);
    }

var derived = new Derived("dname");
console.log(derived.name);
derived.initModule();

A question is, why use 'prototype' at all? We can also do something like Derived = Object.create(Base);

for example (jsfiddle):

Base =
{
    initModule: function() {
        // init on all the modules.
        for (var i = 0; i < this.modules.length; i++)
            this.modules[i].initModule();
        console.log("base initModule",this.name);
    },
    init: function(name) {
        this.name = name; 
        this.modules = [];
    }
};

Derived = Object.create(Base);

Derived.initModule = function () {
      console.log("d init module");
      //  calling base class functionality
      Base.initModule.call(this);
    }
Derived.init("dname");
console.log(Derived.name);
Derived.initModule();
like image 922
Lior Avatar asked Mar 07 '13 16:03

Lior


People also ask

Why we are using prototype in JavaScript?

Whenever we create a JavaScript function, JavaScript adds a prototype property to that function. A prototype is an object, where it can add new variables and methods to the existing object. i.e., Prototype is a base class for all the objects, and it helps us to achieve the inheritance.

Why is prototype inherited?

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.

What is prototype inheritance in JavaScript?

When it comes to inheritance, JavaScript only has one construct: objects. 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.

Should you use prototype in JavaScript?

There is a clear reason why you should use prototypes when creating classes in JavaScript. They use less memory. When a method is defined using this. methodName a new copy is created every time a new object is instantiated.


2 Answers

If you don't use the prototype, the methods are redefined with every class. That is to say, new Base; new Base; new Base will create six functions in the second example. This takes more time and space. Derived will also create its own functions.

Additionally, you can't use the prototype to change methods for each instance on the fly (or add new methods), which could be potentially helpful -- especially across modules.

However that's not to say that you should always use the prototype for every method. Each situation is different.

prototype also allows you to call methods in a different context without creating an instance (as in the case of Array.prototype.forEach.call on an array-like object).

like image 140
Explosion Pills Avatar answered Sep 27 '22 16:09

Explosion Pills


Basically it is quite simple, The Object.create is part of Ecmascript 5, which is quite new. Prototype has been around since the beginning of javascript and is supported by all the browsers.

So as long as you need support for Internet Explorer 7 or 8, you shouldn't be relying on Create. Also see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create

(The article also suggests a polyfill, which uses prototype ;-))

like image 32
Jaco Koster Avatar answered Sep 27 '22 17:09

Jaco Koster