Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created November 27, 2020 01:57
Show Gist options
  • Select an option

  • Save misterpoloy/e02a8ecf3a652fd44568568b042644d9 to your computer and use it in GitHub Desktop.

Select an option

Save misterpoloy/e02a8ecf3a652fd44568568b042644d9 to your computer and use it in GitHub Desktop.
Singleton pattern
let instance = null;
class Printer {
constructor(pages) {
this.display= function(){
console.log(`You are connected to the printer. You want to print ${pages} pages.`)
}
}
static getInstance(numOfpages){
if(!instance){
instance = new Printer(numOfpages);
}
return instance;
}
}
var obj1 = Printer.getInstance(2)
console.log(obj1)
obj1.display()
var obj2 = Printer.getInstance(3)
console.log(obj2)
obj2.display()
console.log(obj2 == obj1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment