0

Is there any standard way for inheritance in javascript? Any standard function which I can use for inheritance, I just pass the two objects(or constructors) and it does the needful.

Sushil Kumar
  • 1,401
  • 2
  • 14
  • 27
  • I have not seen any "helper" function that deals with inheritance in JavaScript completely because it would be too complicated to write such a function and you still need to do a lot of things yourself. To inherit in JavaScript you need to set child.prototype and call the parent constructor in the child constructor `Parent.apply(this,arguments)` When you want to extend a Parent function you can call a Parent function like so `Parent.prototype.someExtendedMethod.call(this,args);` More information can be found here: http://stackoverflow.com/a/16063711/1641941 – HMR Jun 05 '14 at 08:05
  • I am not sure but most of the javascript libraries use a fix helper function for inheritance, where they pass the parent and the child as parameters. – Sushil Kumar Jun 05 '14 at 09:39

3 Answers3

2

If you are using Node.js, then you can use util.inherits funciton, like this

util.inherits(MyStream, events.EventEmitter);

Here, MyStream is the child and events.EventEmitter is the parent.

Otherwise, you can simply use the following code

Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • I am using javascript mainly for DOM manipulation in UI. I think the second one would not work for objects. – Sushil Kumar Jun 05 '14 at 07:21
  • @SushilKumar Can you give an example? The methods I listed are the common ways to inherit. – thefourtheye Jun 05 '14 at 07:25
  • 1
    Before you start to attempt inheritance with DOM objects, read [*What's wrong with extending the DOM*](http://perfectionkills.com/whats-wrong-with-extending-the-dom/) for the "…downsides of this seemingly useful practice". Working with [*native objects*](http://ecma-international.org/ecma-262/5.1/#sec-4.3.6) is fine, but [*host objects*](http://ecma-international.org/ecma-262/5.1/#sec-4.3.8) are a law unto themselves. – RobG Jun 05 '14 at 07:28
  • You are right I think the whole idea of object inheriting from other object is silly. I wrote the first comment because I thought in javacsript an object(instance) inherits from other object and objects (other than functions) do not have prototype proeprty. – Sushil Kumar Jun 05 '14 at 07:34
0

This is the example code generated by Sharpkit compiler for Inheritance, Hope it helps :)

if (typeof($Inherit) == 'undefined') {
    var $Inherit = function(ce, ce2) {
        if (typeof(Object.getOwnPropertyNames) == 'undefined') {
            for (var p in ce2.prototype)
                if (typeof(ce.prototype[p]) == 'undefined' || ce.prototype[p] == Object.prototype[p])
                    ce.prototype[p] = ce2.prototype[p];
            for (var p in ce2)
                if (typeof(ce[p]) == 'undefined')
                    ce[p] = ce2[p];
            ce.$baseCtor = ce2;
        } else {
            var props = Object.getOwnPropertyNames(ce2.prototype);
            for (var i = 0; i < props.length; i++)
                if (typeof(Object.getOwnPropertyDescriptor(ce.prototype, props[i])) == 'undefined')
                    Object.defineProperty(ce.prototype, props[i], Object.getOwnPropertyDescriptor(ce2.prototype, props[i]));

            for (var p in ce2)
                if (typeof(ce[p]) == 'undefined')
                    ce[p] = ce2[p];
            ce.$baseCtor = ce2;
        }
    }
};
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Arun Tyagi
  • 2,206
  • 5
  • 24
  • 37
0

Is there any standard way for inheritance in javascript?

Yes, prototype inheritance. It's in the spec. The basic pattern:

// Constructor
function Foo(name) {
  this.name = name;
}

// Constructor's prototype holds properties inherited by instances
Foo.prototype.sayName = function() { return this.name }

// Use new to create an instance
var foo = new Foo('foo');

alert(foo.sayName()); // foo

You can also emulate various other types of inheritance, but generally it's not a good idea (or to get to fancy with any kind of inheritance in javascript, it just doesn't need it). Sometimes it helps though.

Search the interwebs or here on SO, there are thousands of articles, blogs and answers.

Any standard function which I can use for inheritance

See thefourtheye's answer and Object.create.

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209