Semantic Constructors

Written by on July 18th, 2008 in Uncategorized.

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.

Source: Ajaxian » Front Page
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/338889366/semantic-constructors

Comments are closed.



Site Navigation