Skip to content

Instantly share code, notes, and snippets.

@nmtitov
Created November 15, 2012 22:33
Show Gist options
  • Select an option

  • Save nmtitov/4081895 to your computer and use it in GitHub Desktop.

Select an option

Save nmtitov/4081895 to your computer and use it in GitHub Desktop.
// конструктор родительского класса
var A = function () {
this.x = 10;
this.y = 10;
this.area = function () {
return this.x * this.y;
}
}
// A.prototype.area = function () {
// return this.x * this.y;
// }
var a = new A();
console.log('area of super class', a.area());
var B = function () {
A.call(this); // вызываем конструктор родителя
}
B.prototype = Object.create(A.prototype); // наследуем, т.е. используем прототип родителя
// меняем имплементацию метода в родителе
A.prototype.area = function () {
return Math.pow(this.x, this.y);
}
var b = new B();
console.log('area of child class', b.area());
@nmtitov
Copy link
Author

nmtitov commented Nov 15, 2012

// конструктор родительского класса
var A = function () {
this.x = 10;
this.y = 10;
}

// площадь добавляем к прототипу объекта A
A.prototype.area = function () {
return this.x * this.y;
}

var a = new A();
console.log('area of super class', a.area());

var B = function () {
A.call(this); // вызываем конструктор родителя
}

B.prototype = Object.create(A.prototype); // наследуем, т.е. используем прототип родителя

// меняем имплементацию метода в родителе

A.prototype.area = function () {
return Math.pow(this.x, this.y);
}

var b = new B();
console.log('area of child class', b.area());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment