这就是我遇到的……我设计我的对象:
var Tab = function () {
var _data = [], _owners = [], _supportWorkers = [], _watchers = [];
var _dataLength = function () { return _data.length; };
return {
Data: _data,
Owners: _owners,
SupportWorkers: _supportWorkers,
Watchers: _watchers,
Length: _dataLength
};
};
var newObject = new Tab();
newObject.Data = [{"my":"data"}];
alert(newObject.Length()); // Length == 0
alert(newObject.Data.length); // Length == 1
这显然会更改引用,但不会更改对象内部的引用。创建 Getter/Setter 函数是避免这种情况发生的唯一方法吗?