资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
外文原文来源: Mastering Ajax 14 Aug 2007 From OReilly Media Inc.2JavaScript RefresherJavaScript is an essential piece of the Ajax package. JavaScript serves as the intermediary betweenthe browser (the client) and the server so that a web page can be dynamically updated withoutrefreshing the entire page.JavaScript was developed by Brendan Eich and introduced in the 1995 release of Netscape 2.0.Because JavaScript offered a means to add interactivity to HTML pages, it quickly became popularand widely used. Different versions were developed both by Netscape and other browser manufacturers,but eventually the JavaScript language was standardized by the European ComputerManufacturers Association (ECMA) as ECMAScript.The ECMAScript standard defines the core of the JavaScript language. Most browsers today supportthe third edition of the ECMA-262 standard.This chapter provides a review of core JavaScript (ECMAScript), JavaScript objects, the DocumentObject Model (DOM), and event handling for readers who have some familiarity and experiencewith JavaScript and with basic programming constructs (variables, operators, functions). If yourenew to programming and/or JavaScript, refer to books such as Beginning JavaScript, Second Edition(Indianapolis: Wiley Publishing, 2004) and Professional JavaScript for Web Developers (Indianapolis:Wiley Publishing, 2005) for more comprehensive information about JavaScript.In this chapter, you learn about these topics: Core JavaScript (variables, operators, statements, functions) Object-oriented JavaScript (built-in objects, the Browser Object Model, user-defined objects) The Document Object Model (DOM) JavaScript and eventsCore JavaScriptCore JavaScript (ECMAScript) provides everything you need to accomplish basic programming tasks:variables, operators that act on the variables, statements, and functions that provide reuseable blocks ofcode to accomplish a task. Objects are also part of ECMAScript, and they are discussed later in this chapterin the section, “Object-Oriented JavaScript.”SyntaxThe ECMAScript standard specifies JavaScripts basic syntax rules. Using correct syntax is crucial. Ifyour code has syntax errors, the browsers JavaScript interpreter stops processing your code until theerror is corrected.A few guidelines can help you follow good programming practices and avoid syntax errors: JavaScript is case-sensitive Pay extra attention when using capital letters in the names ofvariables, functions, and objects. A variable named myVar is not the same as one named myvar. Semicolons are optional Even though semicolons are not required at the end of every codestatement, its good practice to include them in your code to delineate where the line of codeends. They make the code easier to read, easier to debug, and they help decrease the number oferrors generated when the browser reads your code. Let the code wrap Dont insert line returns in the middle of a line of code. A line return characterin the middle of a code statement is one of the most common errors in JavaScript, and linereturns will make your code non-functional. Add comments The syntax for JavaScript comments is / for a single-line comment and /*and */ for opening and closing multiline comments. Commenting your code makes it mucheasier for you or anyone else to understand your code when its being updated or debugged.Remember, the person updating or debugging just might be you.VariablesA variable is a temporary storage container for data. JavaScript variables can be declared using the varkeyword. For example, the following declares a new variable named myColor:var myColor;This variable doesnt contain any information yet. If you included this variable in an alert message, suchas the following, its value would be undefined, as shown in Figure 2-1:alert (“myColor = “ + myColor);Figure 2-1: Undefined value.You dont have to declare a variable as a separate step. Instead, you can initialize a variabledeclare itand assign a value to it at the same time, as shown here:var myColor = “green”;Variable names must start with a letter, an underscore (_), or a dollar sign ($). Camel notation is commonlyused for variable names, although no specific notation is required. In camel notation, the firstword is lowercase, and additional words start with a capital letter, as shown here:var myFavoriteNumber;Primitive DatatypesEvery JavaScript variable has a datatype that indicates the kind of data the variable contains. Primitivedatatypes store data directly in a variable. There are five types of primitive datatypes: Number String Boolean Undefined NullThe Number type includes both integers and floating-point numbers. The String type includes any groupof one or more characters. A string is distinguished from a number by enclosing it in quotation marks.For example, “5” is a
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号