Created
November 15, 2012 22:33
-
-
Save nmtitov/4081895 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // конструктор родительского класса | |
| 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()); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// конструктор родительского класса
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());