Skip to content

Instantly share code, notes, and snippets.

@khaled0fares
Last active September 23, 2016 22:21
Show Gist options
  • Save khaled0fares/17996e816e82cc2840fcca18411c589e to your computer and use it in GitHub Desktop.
Save khaled0fares/17996e816e82cc2840fcca18411c589e to your computer and use it in GitHub Desktop.
Singleton pattern in JS
let Singleton = (function(name, yearOfBirth, job, relationship){
let instance;
function init(){
return {
name: name,
age: new Date().getFullYear() - yearOfBirth,
job: job,
relationship: relationship
}
}
if(!instance){
instance = init();
}
return instance;
})("Fares", 1993, "Software engineer", "Single");
console.log(Singleton);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment