0

I'm confused about javascript inheritance.

Consider the following code:

function parent(firstname, lastname) {
    this.firstname = firstname || "abc";
    this.lastname = lastname || "def";
}

function child() {
   this.childname = "xys";
}
parent.prototype.Greetings = function () {
    alert("sayhi");
}
child.prototype = Object.create(parent.prototype);
var child1 = new child();

Now, does the child1 object have access to the firstname and lastname properties? I can access the Greetings method (because it's in the prototype). If I try to access these, it is showing as undefined. What changes have to be made to access these variables?

2
  • Note: It's Object.create, not object.create. Commented Sep 9, 2016 at 18:36
  • Thanks @melpomene for the edits Commented Sep 9, 2016 at 20:18

2 Answers 2

1

What changes has to be done to access these variables?

You have to call the parent constructor in the child constructor:

function child() {
  parent.call(this);
  this.childname = "xys";
}

JavaScript "inheritance" is a lot less magical (i.e. implicit) than in other languages (before ES6 classes at least).

In your example you have a function parent which sets two properties on this. However, nowhere in your code are you calling parent, so these properties will never be set.

In order to set them we need to apply parent to the new child instance, which is done by calling parent.call(this);.

Since parent accepts arguments, you probably want to pass them through child eventually:

function child(firstname, lastname) {
  parent.call(this, firstname, lastname);
  this.childname = "xys";
}

Related: Benefits of using `Object.create` for inheritance

Sign up to request clarification or add additional context in comments.

2 Comments

Is this approach called as constructor stealing? .For inheritance, in javascript we should do a combination of prototypal and constructor stealing..Please correct me if iam wrong
@Geeky: I have never heard of that. But it's the same as calling super() in other languages (and ES6). Having to call the parent constructor is pretty typical for inheritance.
0

A better way to do this is using more recent ES2015 standards. The syntax is much clearer to follow. Do something like:

class Parent {
    constructor(firstname, lastname) {
        this.firstname = firstname || "abc";
        this.lastname = lastname || "def";
    }
    greetings() {
        alert("sayhi");
    }
}

class Child extends Parent {
    constructor(){
        super(firstname, lastname);
        this.childname = "xys";
    }
}
var child1 = new child();

Info on ES2015 can be found at https://babeljs.io/docs/learn-es2015/ . Also, more specifics on class declarations in javascript can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes .

5 Comments

This won't work, you have to call super(this) in the child constructor.
True, I had forgotten the super(this). I updated the answer.
should be constructor(firstname, lastname) { super(firstname, lastname)
Oh whoops, yeah no need to pass this.
That's right, the arguments must be the same as for the parent constructor. Thanks. I'm a little new to js inheritance, as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.