Created
November 14, 2017 21:06
-
-
Save VoloshchenkoAl/db925ce5c02e78f8a376cba023376842 to your computer and use it in GitHub Desktop.
This file contains 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
// --------- Класс-Родитель ------------ | |
// Конструктор родителя пишет свойства конкретного объекта | |
function Animal(name) { | |
this.name = name; | |
this.speed = 0; | |
} | |
// Методы хранятся в прототипе | |
Animal.prototype.run = function() { | |
alert(this.name + " бежит!") | |
} | |
// --------- Класс-потомок ----------- | |
// Конструктор потомка | |
function Rabbit(name) { | |
Animal.apply(this, arguments); | |
} | |
// Унаследовать | |
Rabbit.prototype = Object.create(Animal.prototype); | |
// Желательно и constructor сохранить | |
Rabbit.prototype.constructor = Rabbit; | |
// Методы потомка | |
Rabbit.prototype.run = function() { | |
// Вызов метода родителя внутри своего | |
Animal.prototype.run.apply(this); | |
alert( this.name + " подпрыгивает!" ); | |
}; | |
// Готово, можно создавать объекты | |
var rabbit = new Rabbit('Кроль'); | |
rabbit.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment