DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> 編寫 JavaScript 代碼
編寫 JavaScript 代碼
編輯:JavaScript基礎知識     
與許多其他編程語言一樣,JavaScript 按語句、由相關語句集構成的塊和注釋進行組織。在一個語句內,可以使用變量、字符串、數字和表達式。
語句

JavaScript 程序是語句的集合。JavaScript 語句通過某種方式組合表達式以執行完整任務。

一個語句由一個或多個表達式、關鍵字或運算符(符號)組成。通常,在一個行上書寫一個語句,但可在兩個行或多個行上書寫一個語句。此外,在同一個行上書寫兩個或多個語句,用分號分隔。通常,每個新行都開始一個新語句。最好是顯式終止您的語句。可使用分號 (;) 做到這一點,分號是 JavaScript 語句的終止字符。

以下是 JavaScript 語句的兩個示例。字符 // 後面的語句是注釋,它們是程序中的說明性備注。


var aBird = "Robin"; // Assign the text "Robin" to the variable aBird.
var today = new Date(); // Assign today's date to the variable today.

由括號 ({}) 包圍的一組 JavaScript 語句稱為一個塊。組織在一個塊中的語句通常可以視為一個語句。這就意味著可以在 JavaScript 要求使用單個語句的大多數地方使用塊。需要引起注意的例外情況包括 for 和 while 循環的頭。注意,塊中的單個語句以分號結束,但塊本身不是這樣。

通常,塊在函數和條件中使用。注意,與 C++ 和一些其他語言不同,JavaScript 並不將塊視為一個新范圍;只有函數創建新范圍。

在下面的示例中,else 子句包含由大括號括起來的由兩個語句構成的塊。該塊被視為一個語句。此外,該函數本身由用大括號括起來的語句塊組成。函數下方的語句位於塊外部,因此不是函數定義的一部分。


function inchestometers(inches)
{
if (inches < 0)
return -1;
else
{
var meters = inches / 39.37;
return meters;
}
}

var inches = 12;
var meters = inchestometers(inches);
document.write("the value in meters is " + meters);


注釋

單行 JavaScript 注釋以兩個正斜槓 (//) 開始。以下是單行注釋的一個示例。


var aGoodIdea = "Comment your code thoroughly."; // This is a single-line comment.
多行 JavaScript 注釋以正斜槓和星號 (/*) 開頭,以相反的順序 (*/) 結束。


/*
This is a multiline comment that explains the preceding code statement.

The statement assigns a value to the aGoodIdea variable. The value,
which is contained between the quote marks, is called a literal. A
literal explicitly and directly contains information; it does not
refer to the information indirectly. The quote marks are not part
of the literal.
*/

注意
如果嘗試在一個多行注釋中嵌入另一個多行注釋,JavaScript 將以一種意想不到的方式解釋生成的多行注釋。標記嵌入的多行注釋結尾的 */ 將被解釋為整個多行注釋的結尾。這意味著,不會注釋掉已嵌入的多行注釋後面的文本;相反,它將被解釋為 JavaScript 代碼並將生成語法錯誤。
建議將所有注釋編寫為單行注釋的塊。這樣就允許隨後用一個多行注釋來注釋大段代碼。



// This is another multiline comment, written as a series of single-line comments.
// After the statement is executed, you can refer to the content of the
// aGoodIdea variable by using its name.
var extendedIdea = aGoodIdea + " You never know when you'll have to figure out what it does.";

賦值和等號

等號 (=) 在 JavaScript 語句中用於為變量賦值:它是賦值運算符。= 運算符的左操作數始終為 Lvalue。Lvalue 的示例包括:
變量,
數組元素,
對象屬性。

= 運算符的右操作數始終為 Rvalue。Rvalue 可以是任何類型的任意值,包括表達式的值。下面是 JavaScript 賦值語句的一個示例。


var anInteger = 3;

JavaScript 編譯器將該語句解釋為:“將值 3 賦給變量 anInteger”,或“anInteger 采用值 3。”

一定要確保理解 = 運算符(賦值)和 == 運算符(相等)之間的差異。當您希望比較兩個值以確定二者是否相等時,請使用雙等號 (==)。這將在控制程序流中詳細討論。


表達式

JavaScript 表達式值可以是任何有效的 JavaScript 類型,即數字、字符串、對象等。最簡單的表達式是文本。以下是 JavaScript 文本表達式的一些示例。


3.9 // numeric literal
"Hello!" // string literal
false // boolean literal
null // literal null value
{x:1, y:2} // Object literal
[1,2,3] // Array literal
function(x){return x*x;} // function literal

更復雜的表達式可包含變量、函數調用和其他表達式。可以使用運算符組合表達式來創建復雜的表達式。運算符的示例包括:+(加法)、-(減法)、*(乘法)和 /(除法)。

以下是 JavaScript 復雜表達式的一些示例。


var anExpression = 3 * (4 / 5) + 6;
var aSecondExpression = Math.PI * radius * radius;
var aThirdExpression = aSecondExpression + "%" + anExpression;
var aFourthExpression = "(" + aSecondExpression + ") % (" + anExpression +
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved