【发布时间】:2010-10-28 19:13:04
【问题描述】:
我正在查看http://docs.jquery.com/Plugins/Authoring 的插件创作文章,并在默认值和选项部分看到了这个示例:
$.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 调用移到 return 上方(并用 || {} 替换了丑陋的 if - 我认为这有意义吗?)。
这里有什么我没有看到的吗?任何奇怪的关闭相关的东西可能吗?如:全局修改设置与仅在该调用上? (编辑:显然我不是 - http://jsfiddle.net/fVSDH/)
【问题讨论】:
标签: jquery