1

from https://stackoverflow.com/a/2107571/494461

function Base (string ) {
  this.color = string;
}

function Sub (string) {

}
Sub.prototype = new Base( );


var instance = new Sub ('blue' );

How do I pass the string variable ahead to the base class?

Community
  • 1
  • 1
  • Maybe the following answer can help you with this as well.http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR May 19 '14 at 12:20

1 Answers1

2

Just invoke the Base function, like this

function Base (string) {
    this.color = string;
}

function Sub (string) {
    Base.call(this, string);
}

Using the Function.prototype.call, you are setting the current object to the Base function call as the current object in Sub. So, Base will be actually creating the color property in Sub object only.

Also, the prototype of an Object should depend only on the prototype of other objects, not on others objects. So, you would want to inherit with the common way

Sub.prototype = Object.create(Base.prototype);
thefourtheye
  • 233,700
  • 52
  • 457
  • 497