Skip to content

Instantly share code, notes, and snippets.

@taiju
Created October 24, 2010 03:00
Show Gist options
  • Save taiju/643026 to your computer and use it in GitHub Desktop.
Save taiju/643026 to your computer and use it in GitHub Desktop.
jQueryのようにクラスメソッドをコンストラクタにして、そのままメソッドチェインでメソッドを呼び出しできるようにする
(function() {
var _Hoge = function() {
var that = this;
return function(val) {
that._val = val ? val : null;
return that;
}
};
_Hoge.prototype = {
val: function(v) {
if (v) {
this._val = v;
return this;
}
else {
return this._val;
}
}
}
window.Hoge = new _Hoge();
})();
Hoge(100).val();
// 100
Hoge().val(20).val();
// 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment