10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2<br />

❑<br />

❑<br />

Examples of compiled programs are as simple as the Notepad application you’ll be using to edit<br />

the samples shown in this chapter. Notepad was written in C++ and compiled to run specifically<br />

on 32-bit or 64-bit Intel hardware. Therefore, it is very specific as to what platform can run<br />

the code.<br />

JavaScript on the other hand is interpreted, meaning that any platform capable of interpreting the<br />

code can run it. For our purposes, this will mean virtually any web browser, from small handheld<br />

devices like web-enabled phones up to browsers available for mainstream operating systems<br />

like Internet Explorer, Opera, Safari, and Firefox. Some languages, such as .<strong>NET</strong> and Java, are<br />

actually hybrids, being both partially compiled and partially interpreted.<br />

Although it’s tempting to think that JavaScript is closely related to Java, because of its name, surprisingly<br />

that is not the case. The only real similarities between the two are that the structure of the code<br />

looks very similar, with the curly braces and semicolons, and they both provide an object-oriented programming<br />

experience. Java does this more elegantly and has established itself as a great academic tool to<br />

experiment with object-oriented code design. But the object model is very different from JavaScript.<br />

General Rules of JavaScript<br />

JavaScript code comprises a series of statements. And JavaScript statements are composed of either text<br />

that leads up to a semicolon or text that opens up a code block inside curly braces. Here’s an example of<br />

a couple of statements that first declare a variable called message and then present it on the screen in a<br />

pop-up box:<br />

var message=”Hello, world!”;<br />

alert(message);<br />

The computer determines the end of each of the statement simply by where it finds a semicolon. These two<br />

statements could have been placed on the same line with a semicolon separating them. But it is much<br />

clearer to put them on separate lines.<br />

Most JavaScript interpreters will actually allow statements to exist without the semicolon appearing at<br />

the end of the line. But this is not in keeping with the specifications of other similar languages like Java,<br />

C++, and C#. So, it’s recommended to keep the habit of using semicolons at the end of statements. All of<br />

the JavaScript examples in this book will do so, but when you delve into other samples from the Internet,<br />

you will undoubtedly find JavaScript code that omits the semicolons.<br />

Now take a look at an example of a statement that opens a code block:<br />

if(grade>=90)<br />

{<br />

alert(“You got an A!”);<br />

}<br />

In this sample, the curly braces define a code block, and the single line of code found within runs only if<br />

the grade variable is greater than or equal to 90. There are two statements shown, if and alert, and<br />

there is no concluding semicolon required after the code block. In addition to having code blocks with<br />

an if statement, you’ll see in upcoming samples that they are also found with the looping statement for<br />

and when functions get defined.<br />

12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!