jQuery Profile pluginについて
2008/08/27追記
John Resigのprofile用plugin使った方がいいんじゃね?
John Resig - Deep Profiling jQuery Apps
せつめー
$(selector)の呼び出しのセレクタごとの呼び出し回数と時間を計測して、consoleに出力してくれる。
使い方
- jquery.profile.jsを読み込んでやる。
- $.profile.start();
- $(selector)を呼び出しまくる
- $.profile.done();
とやればstartからdoneの間の$(selector)の呼び出しのセレクタごとの呼び出し回数と時間が、consoleに出力される。
問題
$.profile.start();実行後にjQueryUIが使えなくなる。
他にも使えなくなるpluginはあるんじゃないかなー。
$.profile.startを調べる
コードを読んでみると、コンストラクタであるjQuery.fn.initを無名関数内のローカル変数として保持して、
ラップした関数に差し替えるだけの簡単なお仕事のようです。
jquery.profile.js内でjQuery.fn.initを呼んでいる箇所を見てみる。
result = savedInit.call(jQuery.fn, selector, context),// 91行目 /*中略*/ return result;// 108行目
同じくjquery-1.2.6.js内でjQuery.fn.initを呼んでいる箇所。
return new jQuery.fn.init( selector, context );// 20行目
メソッド呼び出してはなくコンストラクタとしての呼び出しなので、jquery.profile.jsと違う。
jQuery.fn.initについて調べると以下のような記述が…
jQuery.fn.init.prototype = jQuery.fn;// 529行目
実際のインスタンスのprototypeを見てみる
$('div').__proto__;//Object jquery=1.2.6 length=… $('div').__proto__ == jQuery.fn;//true; $.profile.start(); $('div').__proto__;//Object $('div').__proto__ == jQuery.fn;//false; $.profile.done(); $('div').__proto__;//Object jquery=1.2.6 length=… $('div').__proto__ == jQuery.fn;//true;
当たり前だが$.profile.start();すると、
jQueryオブジェクトのprototypeがjQuery.fnではなくObjectになってしまう。
jquery.profile.jsを修正する
先ほどの91行目をこうした。
result = new savedInit( selector, context),// 91行目
直ったっぽい。
とりあえずjQueryUI同梱のFunctional Demosは概ね動く感じになった。