Function inheritence

Hi Campers,

I am working on a rain simulation which involves using sliders to control various properties of the rain. I have made two different objects, a vertical slider and a horizontal slider. I then create new sliders by making a new instance of them like:

function VerticalSlider(x,y,color){
          this.x = x
          this.y = y
          this.color = color
          //etc
}

windSpeedSlider = new Slider(10,100,"blue")

What I would like to do is have something called Slider, which VerticalSlider and HorizontalSlider can inherit properties from. Something like:

function Slider(x,y,color){
          this.x = x
          this.y = y
          this.color = color
          //etc
}

function VerticalSlider inherits Slider(x,y,color){
        //things specific to vertical sliders, but not sliders in general
}

I watched two videos about the prototype chain and experimented a little, but I can’t seem to find the right syntax to achieve this goal. Can someone suggest what to do?

Hi there!
Prototype chain is not difficult,here is sample code that you can learn

function Animal(name) {this.name=name; };
function Bird(name) { this.name=name;};
function Dog(name) { this.name=name;};

Animal.prototype = {
  constructor: Animal, 
  describe: function(extra) {
    console.log("My name is " + this.name,extra);
  }
};

Bird.prototype = {
  constructor: Bird
};
Bird.prototype=Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;

Dog.prototype = {
  constructor: Dog
};
Dog.prototype=Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

let bird=new Bird('Tue');
let dog=new Dog('Bobik');

bird.describe({sts:"other things"});
dog.describe();
1 Like