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
m59
  • 43,214
  • 14
  • 119
  • 136
Krab
  • 6,526
  • 6
  • 41
  • 78
  • 1
    Possible dup of http://stackoverflow.com/questions/7533473/javascript-inheritance-when-constructor-has-arguments and http://stackoverflow.com/questions/7785955/inherit-parent-constructor-arguments and http://stackoverflow.com/questions/3601932/how-do-i-call-an-inherited-javascript-constructor-with-parameters – jfriend00 Dec 14 '13 at 03:32

1 Answers1

0

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

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

B.prototype = new EmptyFunc;
ucdream
  • 691
  • 1
  • 7
  • 18