Last active
April 25, 2024 15:22
-
-
Save DmitriiNazimov/cd319f6d6acada7ea8b52d56c29921d2 to your computer and use it in GitHub Desktop.
Revisions
-
DmitriiNazimov renamed this gist
Aug 30, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
DmitriiNazimov revised this gist
Aug 13, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ * * ПАТТЕРН SINGLETON * * Паттерн Одиночка гарантирует, что класс имеет только один экземпляр, и предоставляет глобальную точку доступа к этому экземпляру. * */ -
DmitriiNazimov revised this gist
Aug 13, 2019 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,11 @@ /** * * ПАТТЕРН SINGLETON * * Паттерн Одиночка гарантирует, что класс имеет толь- ко один экземпляр, и предоставляет глобальную точку доступа к этому экземпляру. * */ var privateMethod = Symbol(); // Чтобы создать приватный метод приходится такую ерунду писать. Ничего лучше пока не придумали. -
DmitriiNazimov revised this gist
Aug 13, 2019 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ Паттерн Одиночка гарантирует, что класс имеет толь- ко один экземпляр, и предоставляет глобальную точку доступа к этому экземпляру. var privateMethod = Symbol(); // Чтобы создать приватный метод приходится такую ерунду писать. Ничего лучше пока не придумали. class Singleton { static #instance = null; // Объявляем статическое приватное свойство. # - значит приватное. -
DmitriiNazimov revised this gist
Aug 11, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Aug 11, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Aug 9, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Aug 9, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Jul 12, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ var privateMethod = Symbol(); // Чтобы создать приватный метод приходится такую ерунду писать. Ничего лучше пока не придумали. class Singleton { static #instance = null; // Объявляем статическое приватное свойство. # - значит приватное. -
DmitriiNazimov revised this gist
Jun 28, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Jun 28, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Jun 28, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov revised this gist
Jun 28, 2019 . No changes.There are no files selected for viewing
-
DmitriiNazimov renamed this gist
Jun 28, 2019 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
DmitriiNazimov created this gist
Jun 28, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ var privateMethod = Symbol(); // Чтобы создать приватный метод приходится такую ерунду писать. Ничего улчше пока не придумали. class Singleton { static #instance = null; // Объявляем статическое приватное свойство. # - значит приватное. constructor(num) { if (Singleton.#instance) { // проверяем что значение #instance не равно null (т.е. уже что-то присвоено), и прерываем инструкцию, чтобы в соответствии с принципом синглтон сохранить значения присвоенные при первой инициации. return Singleton.#instance; } this.state = "justtext"; Singleton.#instance = this; this.publicMethod(num); // автовызов публичного метода в конструкторе. Можно не вызывать в конструкторе, а только вручную this[privateMethod]() // call private } publicMethod(num='default string ') { // Публичный метод с примером дефолтного значения аргумента. console.log('publicMethod: ' + num + this.state); } [privateMethod]() { // Приватный метод, т.е. его нельзя вызывать вне класса. console.log('privateMethod:' + this.state + this.state); } } let first = new Singleton('first'); Singleton.instance = 0; // Попытка внести изменения в приватное свойство не сработает, оно инкапсулировано. let second = new Singleton('second'); second.privateMethod(); // Попытка вызвать приватный метод не сработает. let third = new Singleton('3'); console.log(first === third);