Activate your free membership today | Log-in

Friday, July 18th, 2008

Semantic Constructors

Category: Prototype, Tip

JAVASCRIPT:
  1.  
  2. Class.create = (function(original) {
  3.   var fn = function() {
  4.     var result = original.apply(null, arguments);
  5.     result.toString = function() { return result.prototype.initialize.toString() };
  6.     return result;
  7.   };
  8.   fn.toString = function(){ return original.toString() };
  9.   return fn;
  10. })(Class.create);
  11.  

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:

JAVASCRIPT:
  1.  
  2. Person + ''; // "function klass() { this.initialize.apply(this, arguments); }"
  3.  

too:

JAVASCRIPT:
  1.  
  2. Person + ''; // "function (name) { this.name = name; }"
  3.  

If you want more help with Prototype and Script.aculo.us you can check out their newly changed support list.

Posted by Dion Almaer at 5:37 am

+++--
3.7 rating from 15 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.