MochiKit を使う際に、追加している関数を晒してみる(その1.create_class)

prototype.js の Class.create の模倣+α。

var create_class = function () {
  var m = MochiKit.Base;
  var iter = MochiKit.Iter;

  var new_func = function () {
    if (typeof this._new !== 'function') {
      return;
    }
    this._new.apply(this, arguments);
  };

  iter.forEach(
    arguments,
    m.partial(m.update, new_func.prototype)
  );
      
  return new_func;
};

名前空間を考慮してません。ごめんなさい。

まー、簡単な source なんで、見れば使い方が解るかとは思いますが、一応、下記に用例を示します。

var TestClass = create_class({
  _new : function (message) {
    this.message = message;
  },

  say : function () {
    log(this.message);
  }
});

var test_obj = new TestClass('I love MochiKit.');
test_obj.say();

var TestClassChild = create_class(
  TestClass.prototype,
  {
    say : function (message) {
      log(message + this.message);
    }
  }
);

var test_obj_child = new TestClassChild('I love MochiKit.');
test_obj_child.say('You and ');

その2は、あるのかなー?(partial を予定)