Last active
April 13, 2018 20:31
-
-
Save SergeyLitvin/0a9a8965c9c65a37bd2b243e52e87991 to your computer and use it in GitHub Desktop.
OOP - Functional style
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(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