Chapter 5. Core


All programming languages have to contain structures that allow you to say, "If this is the case, then do that," or to perform calculations, or to define bits of code that should be run repeatedly. These structuresthe structures that actually make JavaScript a programming languageare part of the JavaScript Core language.

In this long chapter, we'll discuss the Core syntax and structures you use most often. For beginners, Core is the most complicated area of JavaScript, partly because some features are technically dense, and partly because their practical use is not always evident. On the other hand, common Core functionalities such as operators, variables, and functions work pretty intuitively.

Although I tried to treat all topics in the (to me) most logical order, readers without programming experience might want to skip some advanced subjects until later. That's quite all right; this chapter isn't supposed to bog you down in details, but to enhance your understanding. If something doesn't work for you, just leave it be for the moment and return to it later on.

You might even decide to skip this chapter entirely, as well as Chapter 6, and move straight to event handling in Chapter 7. Later, when you have more experience, you can always return to Core.

Standards Links

In 1999, Core was standardized by the European Computer Manufacturers Association as ECMAScript. Note that ECMA standardized only JavaScript Core. Most other areas of JavaScript were standardized by W3C.

The ECMAScript specification can be found at http://www.ecma-international.org/publications/standards/Ecma-262.htm.

The most important critique of the specification, by Douglas Crockford, can be found at http://www.crockford.com/javascript/recommend.html.


In order to allow readers without previous programming experience to understand what this chapter is about, we'll start with a quick summary. Don't hesitate to return to this summary if you get lost in the dense technical woods, or to go through this chapter at your own pace and in your own order.

That said, here's the structure of this chapter:

Section A treats a few JavaScript basics. We'll first discuss the statement (one line of JavaScript that gives one specific command). We'll introduce a few important concepts such as operators (characters that give an instruction, like the * character that says "Now multiply this"). A short discussion of values ends this section.

In Section B we'll continue discussing values. From the outset it should be clear that everything in JavaScript, be it a string like "I am a JavaScript hacker!", a number such as 42, an object, or even a function (i.e., a list of JavaScript commands), is in fact a value and can be transferred from one variable to another.

Nonetheless, these values aren't exactly the same; even a non-programmer will see that there's a fundamental difference between the number 42 and a function. Therefore each value has a data type that defines what you can and can't do with it: number, string, boolean, or object.

The beauty of JavaScript is that it's very easy to convert one data type into another; if your script requires you to interpret the number 42 as the string "42", JavaScript silently converts it to a string.

Section C returns to operators and discusses a few important ones, notably those relating to arithmetic (*, /, -, +) and comparisons (==, <=, etc.).

Section D gives the basic rules for creating variables in JavaScript. It also discusses variable scope (i.e., in which area is a variable knownonly in one function or in your entire script?).

Sections E, F, and G discuss working with numbers, strings, and booleans. All these data types have specific features and problems that you should be aware of. Section G gives a detailed overview of data-type conversion to boolean; this conversion comprises the core of object detection, as discussed in 3C.

Section H discusses control structures (if, for, while, do/while, and try/catch). You'll often use these structures for creating script logic, for example "If the user checks this checkbox, do that."

In Section I we'll take a close look at functions, which form the backbone of any script.

Section J discusses JavaScript objects, especially those objects that are created by the browser. For instance, each HTML element in your page is represented by a JavaScript object, and even the simplest script requires you to read out or change this object.

Section K shows that JavaScript objects are really also associative arrays; i.e., arrays that associate a value to a key. This is an important advanced topic; once you master associative arrays, you'll be able to write simpler and more elegant scripts.

Finally, Section L discusses normal arrays (collections of values that are associated with a number instead of a key).



ppk on JavaScript. Modern, Accessible, Unobtrusive JavaScript Explained by Means of Eight Real-World Example Scripts2006
ppk on JavaScript. Modern, Accessible, Unobtrusive JavaScript Explained by Means of Eight Real-World Example Scripts2006
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net