Enavigo

Take lemons, make lemonade or Jill and Yuval’s Musings

Enavigo header image 2

JavaScript Globals

January 17th, 2006 by yuval

If you ever wrote mildly substantial JavaScript, you noticed that that JavaScript is not as premiscuous as it may appear when it comes to namespaces.
If you define a variable outside any block of code, that variable is not necessarily available within the body of functions, say.
The solution is to use the a real global element and ‘attach’ it the global value as property. The object of choice is window and the solution is as simple as
window.myProperty="Yuval";
Thereafter the global value is accessible as
window.myProperty

Very elementary, but I always forget these facts…

Share/Save/Bookmark

Tags: No Comments