2009-08-04
■[JavaScript] JSでthisをそのまま返す関数を作っておくと便利?

DOM操作では以下のように setAttribute してすぐメソッドを呼べない
elm.setAttribute( "name", "value" ).getAttribute( "name" );
なので、引数の関数を実行してthisを返す関数を作ってやると
Object.prototype.expr = function( method ){var args = Array.prototype.slice.call( arguments, 1 );this[ method ].apply( this, args );return this;}; elm.expr( "setAttribute", "name", "value" ).getAttribute( "name" );
微妙だなー。やっぱ jQuery 使うわ。
コメントを書く
トラックバック - http://d.hatena.ne.jp/hasegawayosuke/20090804/p1
リンク元
- 105 http://www.st.ryukoku.ac.jp/~kjm/security/antenna/
- 69 http://www.tfg.ne.jp/~monkey/hatagoya2/
- 64 http://reader.livedoor.com/reader/
- 30 http://d.hatena.ne.jp/amachang/20090803/1249292858
- 14 http://b.hatena.ne.jp/entrylist
- 14 http://blog.lucanian.net/archives/51199862.html
- 13 http://www.kanasansoft.com/weblab/2009/08/jsthis_firefox.html
- 11 http://d.hatena.ne.jp/
- 11 http://www.google.com/reader/view/
- 10 http://mattn.kaoriya.net/software/vim/20090805000133.htm