Created
January 15, 2019 21:47
-
-
Save Dmitriy-8-Kireev/eef707e6555ee8443cbdcb0fac8edbeb to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/qetuwiz
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 characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| // Заставьте это работать | |
| let Dummy = (function () { | |
| let instance = null; | |
| return function Singleton() { | |
| if (instance) { | |
| return instance; | |
| } | |
| if (new.target) { | |
| instance = this; | |
| } else { | |
| return new Singleton(); | |
| } | |
| } | |
| })(); | |
| Dummy.prototype.value = 'fail'; | |
| Dummy.prototype.setValue = function (value) { | |
| this.value = value; | |
| }; | |
| Dummy.prototype.getValue = function () { | |
| return this.value; | |
| }; | |
| // Используем | |
| var foo = new Dummy(); | |
| var bar = new Dummy(); | |
| bar.setValue(123); | |
| // Тесты | |
| console.log('foo === bar ->', foo === bar); // true | |
| console.log('values:', [foo.getValue(), bar.getValue()]); // [123, 123] | |
| // Bonus level | |
| baz = Dummy(); | |
| console.info('baz === bar ->', baz === bar, baz.getValue()); // true, 123 | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">// Заставьте это работать | |
| let Dummy = (function () { | |
| let instance = null; | |
| return function Singleton() { | |
| if (instance) { | |
| return instance; | |
| } | |
| if (new.target) { | |
| instance = this; | |
| } else { | |
| return new Singleton(); | |
| } | |
| } | |
| })(); | |
| Dummy.prototype.value = 'fail'; | |
| Dummy.prototype.setValue = function (value) { | |
| this.value = value; | |
| }; | |
| Dummy.prototype.getValue = function () { | |
| return this.value; | |
| }; | |
| // Используем | |
| var foo = new Dummy(); | |
| var bar = new Dummy(); | |
| bar.setValue(123); | |
| // Тесты | |
| console.log('foo === bar ->', foo === bar); // true | |
| console.log('values:', [foo.getValue(), bar.getValue()]); // [123, 123] | |
| // Bonus level | |
| baz = Dummy(); | |
| console.info('baz === bar ->', baz === bar, baz.getValue()); // true, 123</script></body> | |
| </html> |
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 characters
| // Заставьте это работать | |
| let Dummy = (function () { | |
| let instance = null; | |
| return function Singleton() { | |
| if (instance) { | |
| return instance; | |
| } | |
| if (new.target) { | |
| instance = this; | |
| } else { | |
| return new Singleton(); | |
| } | |
| } | |
| })(); | |
| Dummy.prototype.value = 'fail'; | |
| Dummy.prototype.setValue = function (value) { | |
| this.value = value; | |
| }; | |
| Dummy.prototype.getValue = function () { | |
| return this.value; | |
| }; | |
| // Используем | |
| var foo = new Dummy(); | |
| var bar = new Dummy(); | |
| bar.setValue(123); | |
| // Тесты | |
| console.log('foo === bar ->', foo === bar); // true | |
| console.log('values:', [foo.getValue(), bar.getValue()]); // [123, 123] | |
| // Bonus level | |
| baz = Dummy(); | |
| console.info('baz === bar ->', baz === bar, baz.getValue()); // true, 123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment