LoginSignup
3
3

More than 5 years have passed since last update.

__super__の実装してみる

Posted at

何千番煎じか分からんけど、こんなんを定義する。

Function.prototype.extend = function (Class){
  var f = function (){};
  f.prototype = Class.prototype;
  this.prototype = new f()
  this.prototype.__super__ = Class.prototype;
  this.prototype.__super__.constructor = Class;
  this.prototype.constructor = this;
};

こんな感じで使う。

var BaseClass = function(){
  this.init.apply(this, arguments);
};
BaseClass.prototype.init = function(){
  console.log("base init");
};

var ExClass = function(){
  this.init.apply(this, arguments);
};
ExClass.extend(BaseClass);//継承する
ExClass.prototype.init = function(){
  this.__super__.init();
  console.log("ex init");
};

//
new ExClass();

やってることは継承というかmixinか。
Stateパターンとかやる場合重宝する。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3