Created
October 24, 2010 03:00
-
-
Save taiju/643026 to your computer and use it in GitHub Desktop.
jQueryのようにクラスメソッドをコンストラクタにして、そのままメソッドチェインでメソッドを呼び出しできるようにする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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