-
-
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 |
Yes, because classes are the prototypes, that are then instantiated. For example:
var myClass = zoe.create([zoe.Constructor], {
prototype: {
method: function() {
console.log('shared method');
}
}
});
var p = new myClass();
var q = new myClass();
p.method == q.method;
The extension process alters, because the classes themselves don't share prototypes, but their instances do.
ZOE stands for 'Zest Object Extension'. Zest is a rendering framework soon to be released.
Also note that copying can both be by reference and value. The 'REPLACE' rule for example copies objects and arrays by reference. In this case the memory is shared, with no real issues. The properties are just pointers.
Okay okay, I begin to get it!
But even if p.method == q.method, can method() works on internal properties? (like this.myvalue)
I got a error with your last example : "Uncaught TypeError: Cannot call method 'apply' of undefined"
I also noticed that bug and quickly released a fix!
From the above comment, the base class is just a normal JavaScript constructor, with the prototype set via extension.
So it just looks like -
var myClass = function() {}
myClass.prototype.method = function() {
console.log('shared method');
}
All we did was do the above through a procedural extension instead of in code.
Ok, but your "myClass.prototype" here is a property "prototype" of object and not the real prototype. (like __ proto __)
If I want to call a fonction, I have to myClass.prototype.method() and not directly myClass.method().
Do I miss something?
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'
Automation is awesome! :)
if (val) curVal = val;
it's betterif (!arguments.length) curVal = val;
because of the case you domyObj.hi(0)
The trouble here, you don't use the prototype aspect of js object like http://blog.lightjs.org/2012/08/javascript-part-one-very-object-oriented/ Each time you extend an object, you "copy" all functions to the new one. For few objects it's okay, for hundred of objects less okay.