【发布时间】:2014-06-27 18:43:21
【问题描述】:
不完全确定为什么一个有效而另一个无效。有人可以解释一下吗?我是 JavaScript 新手。到目前为止,我一直在阅读this guide。
这行得通。数据被认为是 _fSettings 对象的局部变量。
ENTRANCE_APP._fSettings = function(){
var data = new StorageObject('settings');
/** The selected camera index. **/
var cameraIndex = data.getValue('cameraIndex','0');
this.setCameraIndex = function(index) {cameraIndex = index;};
this.getCameraIndex = function() {return cameraIndex;};
};
ENTRANCE_APP.settings = new ENTRANCE_APP._fSettings();
但这不是吗?在第一次声明之后,数据被认为是一个全局变量。所以 'data.getValue(...)' 将数据视为全局变量。
ENTRANCE_APP.settings = new function(){
var data = new StorageObject('settings');
/** The selected camera index. **/
var cameraIndex = data.getValue('cameraIndex','0');
this.setCameraIndex = function(index) {cameraIndex = index;};
this.getCameraIndex = function() {return cameraIndex;};
};
【问题讨论】:
标签: javascript function oop encapsulation