Skip to content

Instantly share code, notes, and snippets.

@caasi
Created June 16, 2012 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caasi/2941106 to your computer and use it in GitHub Desktop.
Save caasi/2941106 to your computer and use it in GitHub Desktop.
imagination
var f = function() {
var __kind = "O";
return function(name) {
// private things here
var _vars = {};
_vars["kind"] = function() {
return __kind;
};
_vars["name"] = name || "anonymous";
// then return a new instance
// this should be hide by rewriting
return {
get: function(key) {
return _vars[key];
},
set: function(key, value) {
_vars[key] = value;
}
};
};
};
// first () create a closure with internal vars
// second () create an instance
// well, it also looks like "OO"
var o = f()();
// or in this way
var cls = f();
var p = cls("sam");
o["set"]("x", 10);
p["set"]("x", 20);
console.log("o.name: " + o["get"]("name"));
console.log("o.kind(): " + o["get"]("kind")());
console.log("o.x: " + o["get"]("x"));
console.log("p.name: " + p["get"]("name"));
console.log("p.kind(): " + p["get"]("kind")());
console.log("p.x: " + p["get"]("x"));
// attention: no . notation so far
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment