Showing posts with label crockford. Show all posts
Showing posts with label crockford. Show all posts

Wednesday, April 16, 2008

Global Domination � Yahoo! User Interface Blog

Global Domination � Yahoo! User Interface Blog: "Global Domination
June 1, 2006 at 11:27 am by Douglas Crockford | In Development |

JavaScript, as realized in browsers, is a load-and-go programming language. Programs are delivered to the execution site as text. This is a good fit for the web, which is at its root a text delivery system. The program text is eval‘d, which compiles it into an executable form and then immediately executes it. That execution can leave artifacts in the window’s global object.

The global object is the global namespace that holds the top level functions and global variables. Variables which are not explicitly defined are implied global variables, and their names are also kept in the global object. This was a convenience for the little scripts that Navigator 2 was expected to support. In the decade since NS2, those little scripts have grown into Ajax applications, and the true nature of the global object is revealed:"

Global Domination, Part Two � Yahoo! User Interface Blog

Global Domination, Part Two � Yahoo! User Interface Blog: "Global Domination, Part Two
April 16, 2008 at 1:29 pm by Douglas Crockford | In Development |

As I continue the practice of the craft of programming, I am always examining my practices. Can I improve the patterns that I use so that I can make my programs clearer, stronger, better? This is particularly important when working with a language like JavaScript which has a bias that favors patterns that are confusing, weak, and worse.

One of the worst features of JavaScript is its reliance on global variables.
This can be mitigated with global abatement and the module pattern, which can significantly reduce the number of global variables that we need to declare.

But when we must declare a global variable, how best should we do that?
JavaScript provides three ways of declaring a global variable, and they all
have problems. Which is least worst?"