実装
Object.prototype.chain = function(method) {
this[method].apply(this, Array.prototype.slice.call(arguments, 1));
return this;
};
最後のthis
はthis.valueOf()
でもいいかも知れません。
使い方
document.body.appendChild(
document.createElement("a")
.chain("setAttribute", "href", "http://qiita.com/")
.chain("setAttribute", "target", "_blank")
.chain("setAttribute", "id", "link")
.chain("appendChild", document.createTextNode("Qiita"))
.chain("addEventListener", "click", function() {
//うんたらかんたら
}, false)
);
使い道が思いつかない… (´・ω・`)
with
でいいような気もする。