24 points · by jagtesh · 16y ago · 18 comments
Ruby Hashes and Enumerators have way more functionality than the stock js types.
Is it worth the 40k minimized for the full include? That I dunno.
// code start
var Event = Class.define(function()
{
this.speak = function()
{
console.log("HI");
}
// since you can just write code in here
// metaprogramming is easy:
["attack", "defend"].each(function(name) {
this[name] = function() {console.log(name);}
}.bind(this));
// or build reusable metaprogramming facilities
// and use them here
this.$setters(["x", "y", "z"]);
this.$memoize("expensiveFunc", function() {
// blah
});
this.$inherited = function(derivedClass) { ... }
});Basically, the class define method would create a special 'class-creation' object, and would invoke the user provided function with the class creation object as the this pointer. After the callback finished, I would use the class-creation object to build the actual class.
Being actual able to write code inside your class definition is a much more flexible way to define your objects - and it's also how Ruby does it.
However, I really do think that JavaScript needs all of the great things ECMA 5 displays first and foremost (such as strict mode, getters/setters/enumerable/configurable/writable for property descriptors).
John Resig does an excellent job of summing up ECMA 5 pragmatically (it truly must be great to work on JavaScript stuff as a day job): http://ejohn.org/blog/ecmascript-5-objects-and-properties/
Brings to mind the expression "A bug-for-bug port of the existing system."