(I sometimes go days, if not weeks, at a time before
closing my browser down.)
To avoid memory leaks, you can avoid using closures:
function attachBehavior(){
var element = document.getElementById('main');
element.onclick = onclickHandler;
}
function onclickHandler(){
// the this keyword refers to the element clicked
// and not our variable 'element'
this.innerHTML = 'Surprise!';
}
Many of the JavaScript libraries, especially the ones covered in this book, implement their
scripts to minimize the potential for memory leaks.
Encapsulation
Encapsulation enables you to hide implementation details from those who use your scripts.
Remember the Java code from the beginning of the chapter?
public class Hello
{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
I highlighted in bold how Java enables a developer to show or hide implementation
details from those who use the code. Public methods are an interaction point into the object.
If it were set to private, only the class itself could access those functions. Likewise, if the class
is set to private, only other classes in the namespace could access the class.
When developing code for other people to use, such as within a development team or as
a helper script released to the public, you normally have an application programming interface
(API). There are specific properties and methods that people can use; the rest just make
your life easier from within the script.
Pages:
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129