3
我看在http://docs.jquery.com/Plugins/Authoring插件創作的文章,看到在默認設置和選項部分這個例子:jQuery插件選項:在this.each塊之前還是之內擴展?
$.fn.tooltip = function(options) {
var settings = {
'location' : 'top',
'background-color' : 'blue'
};
return this.each(function() {
// If options exist, lets merge them
// with our default settings
if (options) {
$.extend(settings, options);
}
我不完全理解爲什麼.extend調用是this.each塊裏面?我的第一直覺是將其寫成:
$.fn.tooltip = function(options) {
var settings = {
'location' : 'top',
'background-color' : 'blue'
};
$.extend(settings, options || {});
return this.each(function() {
所以我感動的回報高於.extend通話(如果有|| {}取代了醜 - 我認爲這是有道理的?)。
有什麼我在這裏沒有看到?任何奇怪的關閉相關的東西也許?如下所示:僅在該調用中全局修改設置? (編輯:顯然我不是 - http://jsfiddle.net/fVSDH/)
http://www.learningjquery.com/2007/10/a-plugin-development-pattern – Sarfraz 2010-10-28 19:18:11