-
-
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 |
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'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 -
All we did was do the above through a procedural extension instead of in code.