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
@lightjs
Copy link

lightjs commented Jan 23, 2013

Automation is awesome! :)

if (val) curVal = val; it's better if (!arguments.length) curVal = val; because of the case you do myObj.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.

@guybedford
Copy link
Author

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.

@guybedford
Copy link
Author

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.

@pzeups
Copy link

pzeups commented Jan 23, 2013

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"

@guybedford
Copy link
Author

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.

@pzeups
Copy link

pzeups commented Jan 23, 2013

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?

@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