-
-
Save guybedford/4604409 to your computer and use it in GitHub Desktop.
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 GETSET = { | |
_extend: { | |
properties: 'IGNORE' | |
}, | |
addProperty: function(p, startVal) { | |
var curVal = startVal; | |
this[p] = function(val) { | |
if (arguments.length) | |
curVal = val; | |
else | |
return curVal; | |
} | |
}, | |
_integrate: function(def) { | |
if (def.properties) { | |
for (var p in def.properties) { | |
this.addProperty(p, def.properties[p]); | |
} | |
} | |
} | |
}; | |
// usage: | |
var myObj = zoe.create([GETSET], { | |
properties: { | |
some: 'getters', | |
and: 'setters' | |
}, | |
another: 'property' | |
}); | |
// can then do: | |
myObj.some(); | |
myObj.some('new val'); | |
myObj.some(); | |
myObj.another; | |
myObj.addProperty('hi'); | |
myObj.hi('new hi'); | |
myObj.hi(); | |
// note that myObj can also be implemented into a new object: | |
var extendedObj = zoe.create([myObj], { | |
properties: { | |
extended: 'setter' | |
} | |
}); | |
extendedObj.extended(); | |
extendedObj.some(); | |
extendedObj.and(); | |
// in terms of converting a full object into this format with nesting, | |
// that seems quite expensive and depends on the use case I suppose | |
// ideally the sub properties would in turn be classes themselves |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It behaves just like a standard prototype - when using the
new
keyword, the instance of the prototype has its 'proto' set to the myClass.prototype. So the standard behavior is gained here.In other words you first do
var instance = new myClass()
theninstance.method()
, which accesses the 'proto'