console
var getSingle = function (fn){
let resulet
return function (){
return resulet || (resulet = fn.apply(this,arguments))
}
}
let creartShow = function(){
let div = document.createElement('div')
div.innerHTML = '全局唯一div'
div.style.display = 'none'
document.body.appendChild(div)
return div
}
let creartSingShowLog= getSingle(creartShow)
document.getElementById('button_1').onclick = function(){
let layer = creartSingShowLog()
layer.style.display = 'block'
}
document.getElementById('button_2').onclick = function(){
let layer = creartSingShowLog()
layer.style.display = 'block'
}
class Layer{
constructor(name,age){
this.name = name
this.age = age
}
static getInstance(name,age){
if(!this.instance){
this.instance = new Layer(name,age)
}
return this.instance
}
}
let a = Layer.getInstance('Kevin',29)
let b = Layer.getInstance('Tom',38)
console.log(a)
console.log(b)
console.log(a===b)
<button id="button_1"> 按钮A</button>
<button id="button_2"> 按钮B</button>