これは特に jQuery に当てはまるわけではありませんが、自分のコードでクラスをシミュレートするためのオブジェクト リテラル構文が気に入っています。
http://ajaxian.com/archives/show-love-to-the-object-literal
私はよく、この (簡略化された) フレームワークのようなものを使用する傾向があります。
var Widget = {
sound:'bleep',
load:function(){
// do stuff here that doesn't need to wait until the DOM is ready.
// Inside an anonymous function (such as the 'click' handler below),
// 'this' loses its scope and no longer refers to the widget object.
// To retain a reference to the widget object, assign 'this' to a
// variable. I use an underscore... some people like 'self':
var _ = this;
// when the DOM is ready, run the init "method":
$(document).ready(function(){
_.init(); // the underscore now refers to the widget object
});
},
init:function(){
var _ = this;
// whenever a <p class="noisy"> element is clicked, call makeNoise()
$("p.noisy").click(function(){
_.makeNoise();
});
},
makeNoise:function(){
alert(this.sound); // alert 'bleep'
}
};
Widget.load();
編集:上記の「this」キーワードの使用に関する詳細:
http://groups.google.com/group/jquery-en/browse_thread/thread/328d07f90467cccc?pli=1