Friday, July 18th, 2008

Semantic Constructors

Category: Prototype, Tip

javascript
< view plain text >
  1. Class.create = (function(original) {
  2.   var fn = function() {
  3.     var result = original.apply(null, arguments);
  4.     result.toString = function() { return result.prototype.initialize.toString() };
  5.     return result;
  6.   };
  7.   fn.toString = function(){ return original.toString() };
  8.   return fn;
  9. })(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:

javascript
< view plain text >
  1. Person + ''; // "function klass() { this.initialize.apply(this, arguments); }"

too:

javascript
< view plain text >
  1. 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.

Posted by Dion Almaer at 5:37 am
Comment here

+++--
3.5 rating from 21 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.