Skip to content

Instantly share code, notes, and snippets.

@jmpcyc
Created July 28, 2015 18:59
Show Gist options
  • Select an option

  • Save jmpcyc/d108445a4b194059db17 to your computer and use it in GitHub Desktop.

Select an option

Save jmpcyc/d108445a4b194059db17 to your computer and use it in GitHub Desktop.
JavaScript Single Instance
function Student(name) {
this.name = name;
}
var a = new Student("x");
var b = new Student("y");
Student.name = "z";
console.log(a.name);
console.log(b.name);
var StudentFactory = {};
StudentFactory.getSingleInstance = function(){
if (!this.instance) {
this.instance = new Student("x");
}
return this.instance;
};
var a = StudentFactory.getSingleInstance();
var b = StudentFactory.getSingleInstance();
console.log(a == b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment