Head first JavaScript (four)

Source: Internet
Author: User

If statement

if (test condition)     statement1;else    statement2;

Comments

/* Comments ... */

Script level: Variables declared directly in <script></script> are equivalent to global variables and can be accessed directly within each function. The variables of life within the function can only be accessed within the function but not elsewhere, if possible using local variables.

Switch statement

Switch (test data) {case    ...:        statement;        break;    ...    Default:        statement;        break;}

For loop

for (initial condition; test; update) {    adtion;}

Array var name = new Array ()

Two-bit arrays

var seats = new Array (new Array (5), new Array (5),...);

var showTime = ["12:30", "2:45", "5:00", "8:24"];

While loop

while (test) {    action;    Update;}

Function:reduce, Reuse, recycle

Function name (args) {    body    return value;}

Functions are also variables, function names are variable names, function bodies are variable values

function Showseatstatus (seatnum) {    alert ("...");} var showseatstatus = function (seatnum) {    alert ("...");}; var myshowseatstatus = Showseatstatus;

The above two forms are actually the same, when a function body appears alone without a name, is a so-called literal function (function literal), and functions can be like a variable assignment operation. A variable name such as Myshowseatstatus can be used directly when calling a function, so the function name can also be used as a function reference (functions Reference).

One way to call a function in a Web page is in HTML, such as <body onload = "initseats ();" Another method is to write directly in the JS file, such as Window.onload = Initseats; The onload event is a property of the Window object The second way is that you can separate the HTML from the JS code, without nesting the JS code in the HTML, but at the same time it can make it inconvenient to pass parameters when calling the function. So there's so-called anonymous functions (anonymous function), as follows:

document.getElementById ("..."). onclick = function (evt) {    showseatstatus (...);};

Here the EVT is passed an event object, of course, it does not need to pass parameters can also be called, but the parameter passed is not used.

The OnLoad event can be used to bind a number of events in a page, and can serve as an initialization function for many events:

Window.onload = function () {    //wire other events ...    ...    Initseats ();}

Head first JavaScript (four)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.