Skip to content

Latest commit

 

History

History
137 lines (97 loc) · 2.16 KB

File metadata and controls

137 lines (97 loc) · 2.16 KB

Inheritance


Define a "Class"

Define a "class" using a constructor function

function Shape (x, y) {
  this.x = x;
  this.y = y;
}

Define a "Class"

Define the instance methods augmenting the constructor prototype

function Shape (x, y) {
  this.x = x;
  this.y = y;
}
Shape.prototype.move = function (x, y) {
    this.x += x;
    this.y += y;
};

Shape.prototype.info = function () {
  console.log('x: ', this.x, ', y: ', this.y);
};

Define a "Class"

Instantiate the defined "class" using the operator new

var shape = new Shape(0, 0);

shape instanceof Shape; //true

shape.move(3, 4);
shape.info(); //x: 3, y: 4

Define a sub-"Class"

Define a sub-"Class" using a constructor function
calling the "super" constructor and linking the prototype chain

function Rectangle (x, y, width, height) {
  Shape.call(this, x, y); // call "super" constructor
  this.width = width;
  this.height = height;
}
var rect = new Rectangle(0, 0, 3, 4);
rect.info(); //TypeError: undefined is not a function
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle;
var rect = new Rectangle(0, 0, 3, 4);
rect.info(); //x: 0, y: 0

Define a sub-"Class"

Define the instance methods augmenting the constructor prototype

Rectangle.prototype.area = function () {
  return this.width * this.height;
};

//overriding
Rectangle.prototype.info = function () {
  Shape.prototype.info.call(this);
  console.log('width: ', this.width, ', height: ', this.height);
}

Define a sub-"Class"

Instantiate the defined sub-"class" using the operator new

var rect = new Rectangle(0, 0, 4, 3);

rect instanceof Rectangle; //true
rect instanceof Shape; //true

rect.move(1, 2);
rect.area(); //12
rect.info(); //x: 1, y: 2 width: 4, height: 3

var shape = new Shape();
shape.area(); //TypeError: undefined is not a function

shape instanceof Shape; //true
shape instanceof Rectangle; //false

Object.create

Object.create = function (obj) {
  function F () {}
  F.prototype = obj;
  return new F();
};