Design pattern - singleton
In some scenarios, we will encounter some of the more difficult objects (such as only allowed a pop, allowing only one leader) and so on. This time you need to use a singleton
var Dog = function(name) {
this.name = name
this._instance = null
}
Dog.createDog = function(newName) {
if(this._instance){
this._instance.name = newName
}else{
this._instance = new Dog(newName)
}
return this._instance
}
let husky = Dog.createDog('husky')
let shiba = Dog.createDog('shiba')
console.log(husky.name) // shiba
console.log(shiba.name) // shiba
console.log(husky === shiba) // true
The above case is the easiest mode of single-case model