Skip to content

Commit

Permalink
feat: 单例模式
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjin committed Jan 7, 2020
1 parent cf9245c commit 24f6410
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/javaScript/单例模式.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 单例模式

```js
// 单例模式
function SingLeton(name) {
this.name = name;
}
SingLeton.prototype.getName = function() {
console.log(this.name);
}
SingLeton.prototype.getInstance = (function() {
var instance;
return function(name) {
if (!instance) {
instance = new SingLeton(name);
}
return instance;
}
})();

var a = SingLeton.getInstance(1);
var b =SingLeton.getInstance(2);
console.log(a === b);


// 或者
function S(html) {
this.html = html;
this.init();
}
S.prototype.init = function() {
var div = document.createElement('div');
div.innerHTML = this.html;
document.body.appendChild(div);
}

var Singleton = (function() {
var instance;
return function(html) {
if (!instance) {
instance = new S(html);
}
return instance;
}
})()

var a = new Singleton('test1');
var b = new Singleton('test2');
console.log(a === b)
```

0 comments on commit 24f6410

Please sign in to comment.