Created
June 16, 2012 11:39
-
-
Save caasi/2941106 to your computer and use it in GitHub Desktop.
imagination
This file contains hidden or 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
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