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?
Will work from the outside, since
thiswill 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: