Here's what I've run into... I design my object:
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
This obviously changes the reference, but does not change the reference INSIDE the object. Is creating a Getter/Setter function the only way to avoid this from happening?
var _dataLength = function () { return this.Data.length; };
Will work from the outside, since this
will refer to the instance. It will not work from within the module itself.
Here's how I would do it - the revealing module pattern is pointless if all your data is revealed:
var Tab = function () {
this.data = [];
this.owners = [];
this.supportWorkers = [];
this.watchers = [];
}
Tab.prototype.dataLength = function () { return this.data.length; };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.