random memes }

Programming for the web - Javascript

If you are going to write web applications, you are going to learn Javascript.

In the early web browsers, Javascript was a bit underdone, and caused some problems with security. Entire organizations chose to disallow the use of Javascript in web browsers - and for good reason. In that time frame, when designing a web application, you had to allow for browsers in which Javascript was disabled. That time is gone. Many web applications now require the use of Javascript, and even more will in the future. From this point forward, using a browser with Javascript disabled will be increasingly impractical.

As a side-effect of history, most Javascript examples offered in books and on the web are very badly written. Javascript is a object-oriented language, but nothing like C++ or Java. Want to create an object in Javascript?

function makeCounter(by) { var n = 0; return { next: function() { n += by; return n; } }; } var counter = makeCounter(2);

The variable counter now refers to a unique one-of-a-kind object, and returns a new number each time counter.next() is called. Note there is no declared class, and no need for the new operator. Note the use of a closure. This is Javascript used to full advantage.

Closures give you the leverage you need to greatly simplify you code - which is exactly what we need. The Javascript code attached to a web page needs to be compact, as the code is downloaded and compiled each time the web page is loaded.

Learning to use Javascript is not an option when learning to program web applications. A browser without Javascript is not a viable future option.