HTML and CSS Reference
In-Depth Information
3
This may look like a simple quirk of the language, but it is an incredibly powerful feature.
JavaScript by default is not very good at hiding data. It is not possible to declare properties
as private inside an object. For instance, consider the following object that provides the
same basic “incrementer” functionality:
> obj1 = {i: 0,
increment: function() {
return ++this.i;
}
}
This appears to work correctly:
> obj1.increment()
1
> obj1.increment()
2
> obj1.increment()
3
There is a potential problem here though. Anyone with a reference to this object can change
the current value of i :
> obj1.i = 20
Once this is done, the next call to increment will cause the number to jump to 21:
> obj1.increment()
21
This may be a big problem; it may be an even bigger problem if the following occurs:
Search WWH ::




Custom Search