Skip to content

Instantly share code, notes, and snippets.

@VoloshchenkoAl
Created November 14, 2017 21:06
Show Gist options
  • Save VoloshchenkoAl/db925ce5c02e78f8a376cba023376842 to your computer and use it in GitHub Desktop.
Save VoloshchenkoAl/db925ce5c02e78f8a376cba023376842 to your computer and use it in GitHub Desktop.
// --------- Класс-Родитель ------------
// Конструктор родителя пишет свойства конкретного объекта
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