Skip to content

Instantly share code, notes, and snippets.

@SergeyLitvin
Last active April 13, 2018 20:31
Show Gist options
  • Save SergeyLitvin/0a9a8965c9c65a37bd2b243e52e87991 to your computer and use it in GitHub Desktop.
Save SergeyLitvin/0a9a8965c9c65a37bd2b243e52e87991 to your computer and use it in GitHub Desktop.
OOP - Functional style
// Функция конструктор (способ создания в функциональном стиле ООП) - не приветствуется
function Animal(legs) {
this.legs = legs;
this.walks = function(){
return 'Proudly walks on all ' + this.legs + ' legs';
}
// Создание экземпляра класса
let animal = new Animal(4);
console.log(animal); //{ legs: 4, walks: [Function] }
// Реализация наследования классом
function Cat(legs) {
Animal.apply(this, arguments); // вызов конструктора родителя в контексте созданого объекта Cat
this.purs = function(){// определяем метод специфический только этому классу
return 'Purrrrr';
}
}
var cat = new Cat(4);
console.log(cat);
//{
// legs: 4,
// walks: [Function],
// purs: [Function]
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment