Thursday 12 July 2012

JavaScript isn’t...


JavaScript isn’t. . .

Now that you have an understanding of what JavaScript is (hopefully), let’s take a minute to talk about what JavaScript isn’t. This could also be called the “Myth Busters” section of the chapter!First, JavaScript is not Java. This is a common point of confusion and reasonably so (they both start with “Java,” after all). But, no, JavaScript is not Java.

In fact, JavaScript is unrelated to Java, is a different type of object-oriented language, is a scripting language (Java is compiled), and is used for very different purposes. If you’re going to learn JavaScript, the first thing you must do is stop calling it “Java. ”Second, JavaScript is not just for mouseovers, alerts, and pop-up windows. JavaScript, in the Web browser, is for improving the user experience. Third, JavaScript is not just a client-side technology anymore, although that’s still its primary purpose and use. Over the past couple of years, server-side JavaScript has been developed, in many forms. Fourth, JavaScript is not hard to learn, provided you have the right resource that is!

This web-site treats JavaScript as a true programming language—which it is, providing you with the context and structured approach to help you truly learn, and appreciate, JavaScript. Fifth, JavaScript is not hard to debug. OK, compared to other languages, debugging JavaScript isn’t quite as easy, but given the right tools—see Chapter 3, Tools of the Trade—you can debug JavaScript efficiently. Finally, JavaScript is not a security measure. Because JavaScript is easy for users to disable or manipulate, you should never rely on JavaScript for security purposes.

JavaScriptI never really appreciated the lessons of English grammar until I started studyingforeign languages: Sometimes you just need something to compare and contrastto in order to grasp an idea. In the next couple of pages, I’ll explain how JavaScript compares to other common technologies with which you may be familiar, in the hopes that you can then more fully understand the language you’re about to master.

HTML and CSS

HyperText Markup Language (HTML) is the technology used to create Web pages. (As an aside, if you don’t already know that, you’ll want to learn HTML before goingany further with this web-site. ) HTML is like JavaScript in that both are primarily destinedfor Web browsers, but the comparisons stop there. HTML is a way to presentcontent to users; JavaScript is a way to make that content dynamic.

Cascading Style Sheets (CSS) are also intended for Web browsers, but focus onthe visuals. CSS could be described as somewhat dynamic in that CSS rules canapply differently from one browser to the next, but this is not the same level ofdynamism as JavaScript can offer. CSS, like JavaScript, makes use of the DocumentObject Model (DOM), which is a representation of the HTML found in a page. Infact, the jQuery framework uses CSS-like selectors for its own DOM manipulations.

You may have heard of the MVC (Model, View, Controller) design pattern, whichis an approach to software development that separates the data (called the Model)from the visuals (the View) from the actions (the Controller). In those terms, itmay help to think of HTML as the Model—the data with which you’re dealing,CSS as the View—the presentation, and JavaScript as the Controller—the agentof change and activity.

PHP

PHP is the most popular language used to create dynamic Web sites (and is one ofmy favorite languages). PHP, like JavaScript, is a scripting language, which meanstwo things:

  • Code responds to events
  • Scripts are run through an executable

By comparison, C and C++, among other languages, can be used to write standaloneapplications. Such applications can even take actions on their own, regardlessof events.

The biggest difference between PHP and JavaScript is that JavaScript primarilyruns in a Web browser (aka, a client) and PHP only runs on a server. Whereas theWeb browser includes the JavaScript engine for executing JavaScript code, the Webserver application, such as Apache, includes the PHP module for executing PHP code. Whereas JavaScript reacts to user and browser-based events, PHP reacts to serverbasedevents, such as the request of a particular page or the submission of a form. There is a little overlap as to what the languages can do (e. g. , they can both workwith cookies, generate images, and redirect the Web browser, but the overlaps don’tgo much further). PHP can be used to dynamically generate JavaScript, though, justas PHP can be used to create HTML or CSS on the fly.

PHP can alsobe written taking either a procedural or an object-oriented approach, whereasJavaScript is only an object-oriented language. But both languages are weakly typed. All that being said, if you already know PHP, JavaScript should be comparativelyeasy to learn. As Web programmers are now repeatedly expected to know how todo both client-side and server-side programming, it’s appropriate to learn both. In this web-site, PHP will be used for any server-side needs, such as in the Ajax examples,but you do not need to be a PHP master to follow along with those examples.

Flash

I include Flash in the list of technologies to compare and contrast to JavaScriptbecause Flash is often an alternative to JavaScript for adding dynamic behaviorto Web pages. Modern Web sites, which respond better to user interaction, communicatewith servers, and more, are really Web applications, and are often calledRich Internet Applications (RIAs). RIAs are primarily created using either JavaScriptor Flash. Flash is a proprietary technology managed by Adobe that can be createdin a couple of different ways (Flash itself is not a programming language).

Although Flash can be used for many of the same purposes as JavaScript, howFlash works in the Web browser—it requires a Flash Player plugin—is a key difference. Whereas JavaScript can interact with the HTML page via the DOM, Flashcontent is really separate from the HTML page itself (although JavaScript can beused to communicate between Flash and the Web browser). Also, Flash has complicationswhen it comes to mobile devices, accessibility, and other nontraditionalWeb experiences. All that being said, there’s an argument to be made that themost advanced RIAs—such as games, presentation of lots of data using chartsand graphs, and so forth—can be more quickly and reliably created in Flash. But,again, not everyone can run Flash…

ActionScript

ActionScript is the programming language of Flash and Flex (Flex is a framework forcreating Flash content). ActionScript is extremely similar to JavaScript, as both arederived from the same parent: ECMAScript. But while both languages are objectoriented,ActionScript is strongly typed and is not prototype-based (i. e. , you candefine classes in ActionScript). Still, if you know ActionScript, it will be easy topick up JavaScript, and vice versa.

No comments:

Post a Comment