我有一个我正在创建的 javascript 类。此类具有私有和公共功能/属性。我对私有和公共的理解是,它this
是公共的,并且var
对于该功能及其成员来说是私有的。但是,在本地函数buildFramework()
中,当我调用时var settings.currentView
出现错误:
settings.currentView
没有定义
this
我的问题是,函数及其var
成员的范围以及全局范围之间有什么区别?
namespace('example');
example.InstagramViewer = function (options) {
// this works when called within buildFramework()
this.settings = $.extend({
currentView: 'grid'
}, options);
// this doesn't work when called within buildFramework()
var settings = $.extend({
currentView: 'grid'
}, options);
var viewer;
this.init = function () {
buildFramework();
};
var buildFramework = function() {
viewer = $(viewerWrapper).append('<div id="instagramViewer" class="' + settings.currentView + '"></div>'); // this doesn't work
viewer = $(viewerWrapper).append('<div id="instagramViewer" class="' + this.settings.currentView + '"></div>'); // this does work
};
}
并这样称呼...
$(function () {
var viewer = new connectionsAcademy.publicWebsite.web.js.teenWebsite.InstagramViewer();
viewer.init();
});