Created
July 17, 2012 20:18
-
-
Save basecode/3131784 to your computer and use it in GitHub Desktop.
non-writable factory functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class+constructor | |
var Lib = function() { | |
// instance variable | |
this.aVariable = 0; | |
// instance method | |
this.changeVariable = function(value) { | |
this.aVariable = value; | |
return this; | |
}; | |
} | |
// factory | |
Lib.factory = Object.create(null, { | |
fork: { | |
value: function(value) { | |
return new Lib().changeVariable(value); | |
} | |
} | |
/* more factory functions */ | |
}); | |
with(Lib.factory) { | |
console.log(fork); | |
var fork = ""; | |
console.log(fork(150).aVariable); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment