0
var A = function () {
    this.p1 = 2;
};
A.prototype.f1 = function () {
    return 7;
};
var B = function () {
    inherit(A, B);
};


function inherit(Child, Parent) {
    Child.prototype = Object.create(Parent.prototype);
    Child.prototype.constructor = Child;
}

var b = new B();
console.log(b.p1); // get undefined here 

I am new to JS, sorry for dump question. I would like to inherit B from A. What am I doing wrong?

3 Answers 3

2

You're only calling inherit() after creating the instance of B.

You need to call inherit() statically, once, after defining both functions.

You also need to call A on your instance in B.

For more details on how to properly do inheritance, see my blog.

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

1 Comment

"You also need to call A on your instance in B." Perhaps show how to do that, as it's non-trivial (well, trivial to do, but not trivial to know how to do it). (I'm sure the link covers it, but...)
1

What am I doing wrong?

Two things:

  1. You're calling inherit inside B. You should be doing it outside.

  2. Inside B, you should be calling A, e.g.

    A.call(this/*, other, args, here, if, needed*/);
    

    or

    A.apply(this, arguments);
    

    to pass on all of the arguments B received at runtime via the automatic arguments pseudo-array.

Like so:

var A = function () {
    this.p1 = 2;
};
A.prototype.f1 = function () {
    return 7;
};
var B = function () {
    A.call(this);        // <==== Added
};
inherit(A, B);           // <==== Moved

function inherit(Child, Parent) {
    Child.prototype = Object.create(Parent.prototype);
    Child.prototype.constructor = Child;
}

var b = new B();
console.log(b.p1); // get 2 here now

2 Comments

Thanks for answer! T.J. Crowder, could you please explain just in few words what happens when invoke A.call(this);
@Rudziankoŭ: When you do A.call(x, y, z), it calls the function A in a way that makes this during A's code be x, and then passing along y and z as arguments. It does basically what foo.bar(y, z) does (making this inside bar be foo), but without having to involve an object property expression. More on MDN. (And A.apply is exactly like A.call except that if you want to pass arguments, you give them as an array-like object instead of individual args like you do wih call.)
1

You didn't call the base constructor. Also, it's enough if you inherit only once the classes.

var A = function () {
    this.p1 = 2;
};
A.prototype.f1 = function () {
    return 7;
};
var B = function () {
    A.apply(this, arguments);
};
inherit(A, B);

function inherit(Child, Parent) {
    Child.prototype = Object.create(Parent.prototype);
    Child.prototype.constructor = Child;
    
}

var b = new B();
console.log(b.p1); // get undefined here 

Comments

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.