JavaScript 的语法

原创 2004年08月20日 15:29:00
 

JavaScript 的语法

JavaScript statements consist of keywords used with the appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semi-colon.

Syntax conventions: All keywords in syntax statements are in bold. Words in italics represent user-defined names or statements. Any portions enclosed in square brackets, i.e. [ and ], are optional. {statements} indicates a block of statements, which can consist of a single statement or multiple statements delimited by a curly braces.

The following statements are available in JavaScript:

break comment continue for for...in function if...else return var while with

break statement

The break statement terminates the current while or for loop and transfers program control to the statement following the terminated loop.

Syntax

break

Examples

The following function has a break statement that terminates the while loop when i is 3, and then returns the value 3 * x.

function func(x) { 
    var i = 0; 
    while (i < 6) { 
        if (i == 3)  
            break;
        i++;
    }
    return i*x; 
} 

comment statement

Comments are notations by the author to explain what the script does, and they are ignored by the interpreter. JavaScript supports Java-style comments:

Comments on a single line are preceded by a double-slash (//). Comments that span multiple lines are preceded by a /* and followed by a */.

Syntax

1. // comment text 
2. /* multiple line comment text */

Examples

// This is a single-line comment.


/* This is a multiple-line comment. It can be of any length, and you can put whatever you want here. */

continue statement

The continue statement terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration. In contrast to the break statement, it does not terminate the execution of the loop entirely: instead,

  • In a while loop it jumps back to the condition.
  • In a for loop it jumps to the update expression.

Syntax

continue

Examples

The following example shows a while loop that has a continue statement that executes when the value of i is 3. Thus, n takes on the values 1, 3, 7, and 12.

i = 0; 
n = 0; 
while (i < 5) { 
    i++; 
    if (i == 3) 
        continue;  
    n += i;
} 

for statement

A for loop consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements executed in the loop. The parts of the for statement are:

  • The initial expression, generally used to initialize a counter variable. This statement may optionally declare new variables with the var keyword. This expression is optional.
  • The condition that is is evaluated on each pass through the loop. If this condition is true, the statements in the succeeding block are performed. This conditional test is optional. If omitted, then the condition always evaluates to true.
  • An update expression generally used to update or increment the counter variable. This expression is optional.
  • A block of statements that are executed as long as the condition is true. This can be a single statement or multiple statements. Although not required, it is good practice to indent these statements four spaces from the beginning of the for statement.

Syntax

for ([initial expression]; [condition]; [update expression]) {
    statements
}
initial expression = statement | variable declartion

Examples

This simple for statement starts by declaring the variable i and initializing it to zero. It checks that i is less than nine, and performs the two succeeding statements, and increments i by one after each pass through the loop.

for (var i = 0; i < 9; i++) { 
    n += i;
    myfunc(n);
}

for...in statement

The for statement iterates variable var over all the properties of object obj. For each distinct property, it executes the statements in statements.

Syntax

for (var in obj) { 
    statements }

Examples

The following function takes as its argument an object and the object's name. It then iterates over all the object's properties and returns a string that lists the property names and their values.

function dump_props(obj, obj_name) { 
    var result = "", i = "";
    for (i in obj)
        result += obj_name + "." + i + " = " + obj[i] + "/n";
    return result;
} 

function statement

The function statement declares a JavaScript function name with the specified parameters param. To return a value, the function must have a return statement that specifies the value to return. You cannot nest a function statement in another statement or in itself.

All parameters are passed to functions, by value. In other words, the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

Syntax

function name([param] [, param] [..., param]) {
    statements }

Examples

//This function returns the total dollar amount of sales, when 
//given the number of units sold of products a, b, and c.
function calc_sales(units_a, units_b, units_c) {
    return units_a*79 + units_b*129 + units_c*699
}

if...else statement

The if...else statement is a conditional statement that executes the statements in statements if condition is true. In the optional else clause, it executes the statements in else statements if condition is false. These may be any JavaScript statements, including further nested if statements.

Syntax

if (condition) {
    statements
} [else {
    else statements
}]

Examples

if ( cipher_char == from_char ) {
    result = result + to_char; 
    x++
} else  
    result = result + clear_char; 
 

return statement

The return statement specifies the value to be returned by a function.

Syntax

return expression;

Examples

The following simple function returns the square of its argument, x, where x is an number.

function square( x ) { 
    return x * x; 
} 

var statement

The var statement declares a variable varname, optionally initializing it to have value. The variable name varname can be any legal identifier, and value can be any legal expression. The scope of a variable is the current function or, for variables declared outside a function, the current application.

Using var outside a function is optional; you can declare a variable by simply assigning it a value. However, it is good style to use var, and it is neccessary in functions if there is a global variable of the same name. So, in general, it is a good idea to always use var, but you should definitely use it when declaring a local variable in a function, to ensure that any global variable of the same name does not override it.

Syntax

var varname [= value] [..., varname [= value] ]

Examples

var num_hits = 0, cust_no = 0


while statement

The while statement is a loop that evaluates the expression condition,and if it is true, executes statements. It then repeats this process, as long as condition is true. When condition evaluates to false, execution continues with the next statement following the statements.

Although not required, it is good practice to indent the statements a while loop four spaces from the beginning of the for statement.

Syntax

while (condition) {
    statements
}

Examples

The following simple while loop iterates as long as n is less than three. Each iteration, it increments n and adds it to x. Therefore, x and n take on the following values

  • After first pass: x = 1 and n = 1
  • After second pass: x = 2 and n = 3
  • After third pass: x = 3 and n = 6

After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.

n = 0; 
x = 0; 
while( n < 3 ) { 
    n ++; x += n; 
} 

with statement

The with statement establishes object as the default object for the statements. Any property references without an object are then assumed to be for object. Note that the parentheses are required around object.

Syntax

with (object){ 
     statements 
}

Examples

with (Math) { 
    a = PI * r*r
    x = r * cos(theta)
    y = r * sin(theta) 
}

javascript笔记--(第三章)javascript语法,关键字,保留字

语法构成 区分大小写 ECMAScript中的一切,包括变量、函数名和操作符都是区分大小写的。例如:text和Text表示两种不同的变量。 标识符 所谓标识符,就是指变量、函数、属性的名字,或者函数...
  • a409051987
  • a409051987
  • 2017年01月31日 23:24
  • 306

java与javascript语法区别

java与javascript语法字符 java中变量有明确的类型 JavaScript中变量则是动态类型 8个比特位表示1个字节 1B 1KB = 1024B...
  • qq_37198748
  • qq_37198748
  • 2017年04月22日 23:23
  • 867

Javascript语法规范

Javascript语法规范 1.js文件 不推荐直接写在页面中,除非代码为单独会话,文件载入放在之前。(由于我们使用seajs模块化开发,只需要载入seajs即可)。 2.缩进       ...
  • blue1011372655
  • blue1011372655
  • 2015年01月15日 10:45
  • 1138

JavaScript核心语法一览

本文介绍了JavaScript的基本(常用)语法,供大家参考。 Overview 先说一下Javascript该怎么写。两种形式,一种是直接把JavaScript写在HTML页面里,另一种是存...
  • iamshaofa
  • iamshaofa
  • 2012年08月02日 11:39
  • 9264

javascript语法(一)

1)    引子 前不久我建立的技术群里一位MM问了一个这样的问题,她贴出的代码如下所示: 1 2 3 4 5 6 7 8...
  • u010408365
  • u010408365
  • 2014年12月19日 11:31
  • 690

奇特的js语法

我想,大多数的学习后台语言后再学习js的语法时,都会像我一样,对某些语法有所困惑,因为js的语法相对c#和java来说,不太严谨了,为了灵活却缺失了严谨。我觉得这样做不太好,造成各人写的代码风格都不一...
  • dongliang_shali
  • dongliang_shali
  • 2016年01月30日 18:41
  • 906

诡异的JavaScript语法

前几天一个同事问我如下的JavaScript代码如何理解: (function($){})(jQuery); 其实比较简单,要理解几个概念:1、(),在JavaScript里()表示执行一个方法,如:...
  • KimSoft
  • KimSoft
  • 2009年06月15日 16:31
  • 1582

es6 javascript的 class 类的基本语法

1 概述JavaScript 语言的传统方法是通过构造函数, 定义并生成新对象。 下面是一个例子。function Point(x, y) { this.x = x; this.y = y; } ...
  • qq_30100043
  • qq_30100043
  • 2016年12月09日 10:28
  • 2349

被坑了,js语法跟Java面向对象语法还是有区别的

请见代码: 被坑了一个下午,
  • lutinghuan
  • lutinghuan
  • 2014年11月12日 10:23
  • 1534

JavaScript语法、关键字、变量

JavaScript语法、关键字、变量
  • Liuyuelinjiayou
  • Liuyuelinjiayou
  • 2016年09月22日 23:34
  • 298
收藏助手
不良信息举报
您举报文章:JavaScript 的语法
举报原因:
原因补充:

(最多只允许输入30个字)