HTML and CSS Reference
In-Depth Information
> this
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
Strictly speaking this is not true: it is only true of functions executed in a browser
environment. JavaScript written in server side applications will use a different
object than window for the global scope.
In fact, this function could have been written as follows:
> function getNumber() {
this.num = 10;
return this.num;
The window object is a global object that contains information about the browser envir-
onment, but is also the object that properties are added to by default within functions. The
window object is available to any code in the web page (including external libraries), and
therefore it is referred to as global scope.
Adding properties to the window object is dangerous in any moderately sized application.
Since the global properties can be read and written by any piece of code, there is a signific-
ant possibility that other code will accidentally overwrite your variables.
If you do need to create global variables (and the sample application will create
a couple), it is often a good idea to create a single object in the window to hold
all the variables for your application:
> window.THEAPP = {};
> window.THEAPP.myproperty = 'myvalue ';
As mentioned earlier, the this variable takes on a different meaning inside an object's meth-
> obj = { num: 10,
Search WWH ::

Custom Search