I often use this strategy to my java code in order to make a Collection read only for the outside world, but avoid big/often clonings:
public abstract class MyClass {
List<Long> myIds;
public Collection<Long> getIds() {
return Collections.unmodifiableCollection(this.myIds);
}
}
I would like to follow the same pattern in my JS classes. This would make my business logic code much safer and cleaner, since I would be able to controll every change to my lists (push/splice etc) within the class that owns the fields.
At the moment use "private" fields for the lists and get-set functions for accessing them from outside. The only missing link is some equivalent to java's Collections.unmodifiableCollection. Something that would not copy the whole list (such as slice()), and that would not affect the original field (such as Object.freeze()).
Is there such a feature in JS? If not, how could someone achieve a similar effect (custom iterables?)