【发布时间】:2014-02-11 23:32:12
【问题描述】:
在下面的myFeature 对象的init 函数中有$.extend(myFeature.config, config) 行。我的背景让我想知道为什么该行不是 $.extend(this.config,config)? this 不会引用函数主体中的父对象吗?
如果不是这样,我想我会想使用显示模块模式,如果我理解得足够好的话,因为我可以这样做 var self = this; 并在整个过程中引用 self myFeature 的属性。我不明白在对象文字模式中 init 属性如何访问函数中的 myFeature 。似乎 myFeature 是全局范围的,任何人都可以随时访问它的所有属性和本身,我知道这是 javascript 的本质,我了解一点,但我认为揭示模块模式将为我提供私有/公共访问权限,这使得我晚上睡得更好。
var myFeature = {
config : {
wrapper : '#myFeature',
container : 'div',
urlBase : 'foo.php?item='
},
init : function(config) {
$.extend(myFeature.config, config);
$(myFeature.config.wrapper).find('li').
each(function() {
myFeature.getContent($(this));
}).
click(function() {
myFeature.showContent($(this));
});
},
buildUrl : function($li) {
return myFeature.config.urlBase + $li.attr('id');
},
getContent : function($li) {
$li.append(myFeature.config.container);
var url = myFeature.buildUrl($li);
$li.find(myFeature.config.container).load(url);
},
showContent : function($li) {
$li.find('div').show();
myFeature.hideContent($li.siblings());
},
hideContent : function($elements) {
$elements.find('div').hide();
}
};
$(document).ready(function() { myFeature.init(); });
【问题讨论】:
-
如果你愿意,可以使用 this.config。
-
一个很好的解释等着你,[点击这里][1] [1]:stackoverflow.com/questions/1600130/…
标签: javascript jquery