Semantic Constructors
Date : 2008 07 18 Category : Tech & Development
PLAIN TEXT
JAVASCRIPT:
Class.create = (function(original) {
var fn = function() {
var result = original.apply(null, arguments);
result.toString = function() { return result.prototype.initialize.toString() };
return result;
};
fn.toString = function(){ return original.toString() };
return fn;
})(Class.create);
This monkey patch by kangax allows you to get sense from inspecting a constructor setup via Prototype.
His code changes a simple person class from:
PLAIN TEXT JAVASCRIPT: Person + ''; // "function klass() { this.initialize.apply(this, arguments); }"too:
PLAIN TEXT JAVASCRIPT: Person + ''; // "function (name) { this.name = name; }"If you want more help with Prototype and Script.aculo.us you can check out their newly changed support list.