Last active
February 7, 2022 08:28
-
-
Save jinnyMcKindy/8a5b9912d47c0cfdfa7ef28f7f61d596 to your computer and use it in GitHub Desktop.
OOP + Functional
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
//называем класс согласно сущности, которую он представляет | |
class Fruit { | |
taste = «juicy»; | |
name; | |
constructor(name) { | |
this.name = name; | |
} | |
public getTaste() { | |
return `${this.name} is ${this.taste}`; | |
} | |
} | |
const fruit = new Fruit(«apple»); | |
fruit.getTaste(); //apple is juicy | |
fruit.name = «banana» //oops | |
fruit.getTaste(); //banana is juicy | |
Fruit.getTaste = null; //нарушилась инкапсуляция | |
class Fruit { | |
taste = "juicy"; | |
constructor(name) { | |
this.name = name; | |
} | |
#getTaste() { | |
return `${this.name} is ${this.taste}`; | |
} | |
getPrivateMessage() { | |
return this.#getTaste() | |
} | |
} | |
//называем функцию по действию, которое она делает | |
function getFruitTaste(name) { | |
const taste = «juicy» | |
return function() { | |
return `${name} is ${taste}`; | |
} | |
} | |
getFruitTaste(«apple»)() | |
// чистая функция, функция высшего порядка - возвращает другую функцию |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment