0

I am using this type of code for inheritance. The problem is i am getting error when set up prototype of B, because new A() tries to access params.param and params is undefined when setting up B.prototype. How to deal with it? Or i should use another style for simulating inheritance in js?

function A(params) {
  this.param = params.param; // this will throw error when set up B prototype
}

function B(params) {
  A.call(this, params); // this is ok
}

B.prototype = new A(); // this is bad, A need some parameters
1

1 Answer 1

0

you can do this way and won't get the problem.

var EmptyFunc = function() {};
EmptyFunc.prototype = A.prototype;

B.prototype = new EmptyFunc;
Sign up to request clarification or add additional context in comments.

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.