Skip to content

Instantly share code, notes, and snippets.

@Dmitriy-8-Kireev
Created January 15, 2019 21:47
Show Gist options
  • Select an option

  • Save Dmitriy-8-Kireev/eef707e6555ee8443cbdcb0fac8edbeb to your computer and use it in GitHub Desktop.

Select an option

Save Dmitriy-8-Kireev/eef707e6555ee8443cbdcb0fac8edbeb to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/qetuwiz
<!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>
// Заставьте это работать
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