有一段時間了,我一直在使用jQuery在我的網站上通過JavaScript提供增強功能。但是,我現在開始思考 - 從性能角度來看 - 組織我的函數文件的最佳方式是什麼?將jQuery增強功能添加到網站的最佳方式是什麼?
到現在爲止,我已經做了類似下面的東西:
$(document).ready(function() {
foo();
bar();
});
function foo() {
// do something here
};
function bar() {
// do something else here, and so on...
};
不過,我後來看到的JavaScript文件的佈局已經是這樣的:
$(document).ready(function() {
$(selector).foo();
$(selector).bar();
});
$.fn.foo = function() {
$(this).each(function() {
// do something here and return
});
};
$.fn.bar = function() {
$(this).each(function() {
// do something here and return
});
};
是更好的做法如第二個示例中那樣擴展jQuery並將方法分配給選擇器?或者,將單個函數定義爲第一個例子還是可以的嗎?
謝謝。這是一個相當大的社交網站,我期待提高它的性能(它屬於公司,我是指定的開發人員,所以不要因爲試圖創建下一個Facebook而煩惱);-)') – 2010-07-24 13:19:26
啊我最近一直對此感興趣。在這裏發現了一些有用的帖子,我昨天問了一個問題也出現了一些很好的信息: http://stackoverflow.com/questions/46214/good-ways-to-improve-jquery-selector-performance http:///stackoverflow.com/questions/3316113/why-do-i-have-to-use-this-closed 這也是很好的信息: http://www.artzstudio.com/2009/04/jquery -performance-rules/ – 2010-07-24 13:24:53