Skip to content

Instantly share code, notes, and snippets.

@guybedford
Forked from lightjs/gist:4604334
Last active December 11, 2015 12:58
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 guybedford/4604409 to your computer and use it in GitHub Desktop.
Save guybedford/4604409 to your computer and use it in GitHub Desktop.
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
@guybedford
Copy link
Author

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() then instance.method(), which accesses the 'proto'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment