JavaScript初學基本概念和文法

來源:互聯網
上載者:User

標籤:char   let   sync   condition   rac   ansi   arch   efault   布爾   

ECMAScript 的文法大量借鑒了C及其他類C語言(如Java 和Perl)的文法。

1. 區分大小寫

2.標識符 

  2.1 第一個字元必須是一個字母、底線(_)或一個貨幣符號($);

  2.2 其他字元可以是字母、底線、貨幣符號或數字

  按照慣例,ECMAScript標識符採用駝峰大小寫格式,也就是第一個字母小寫,剩下的每個單詞的首字母大寫。

3. 注釋

  與C語言同  

  // 單行注釋

  /*只是一個多行

   *注釋

  */

4. strict 模式 “use strict”

5. 語句 以一個分號結尾;如果省略分號,則由解譯器確定語句的結尾,雖然語句結尾的分號不是必須的,但建議任何時候都不要省略它。

6. 關鍵字和保留字

break do instanceof typeof case else new var catch finally return void continue for switch while debugger* function this with default if throw delete in tryabstract enum int short boolean export interface static byte extends long super char final native synchronized class float package throws const goto private transient debugger implements protected volatile double import public

 7.變數定義

  var message="hi";

  var value=1.0;

8.資料類型

 5種基礎資料型別 (Elementary Data Type):Undefined,Null,Boolean,Number,String,還有一種複雜資料類型Object

ECMAScript不支援任何建立自訂類型的機制,二所有值最終都將是上述6種資料類型之一。

typeof 操作符

var message="hello";  console.log(typeof(message));  -->string

var value=100.9; console.log(typeof(value)); -->number

undefined 類型, 未經初始化的值預設都會取得undefined的值。

null類型,使用typeof 操作返回Object

Boolean類型

Number類型

Number.MAX_VALUE, Numer.NEGATIVE_INFINITY,Number.POSITIVE_INFINITY

NaN (Not a Numebr 非數值)

Number()函數轉換規則:

a。如果是Boolean值,true和false分別被轉化為1和0

b。如果是數字值,只是簡單的傳入和返回

c。如果是null值,返回0

d。如果是undefined,返回NaN

e。如果是字串,會做類似C語言的轉換,轉換不成功,返回NaN

f。如果是對象,則調用對象的valueOf()方法,然後依據前面的規則轉換返回的值。如果轉換的結果是NaN,則調用對象的toSring()方法,然後再次依照前面的規則轉換返回的字串值。

Object類型

var o=new Object();

如果不給建構函式傳遞參數,可以省略後面的一對圓括弧,如 var o=new object;

Object的每個執行個體都具有下列屬性和方法。

constructor, hasOwnProperty(propertyName), isPrototypeOf(object), propertyIsEnumerable(propertyName),toLocaleString, toString,valueOf.

操作符 ++, --, +, - , *,/, %, &, | , ~ , << , >>, >>>

布爾操作符

!,&&,||

關係操作符

<,>,<=,>=, ==,!=,===,!==

 

語句:

if (contition) statement1 else statement2

 

if (condition1) statement1

  else f (condition2)

   statement2

  else statement3

 

do{ statement

} while (expression);

 

while (expression) statement;

for (initialization ;expression; post-loop-expression) statement;

for (property in expression) statement;

建議在使用for-in迴圈前,先檢測確認該對象的值不是null或undefined。

label語句: 使用label語句可以在代碼中添加標籤,以便將來使用。

label: statement;

break和continue 與C語言同,在迴圈體中控制碼執行。

with 語句,主要目的是為了簡化多次編寫同一個對象的工作。(strict 模式下不允許使用with語句)

with (expression) statement

var qs=location.search.substring(1);

var hostname=location.hostname;

var url=location.href;

上面的代碼都包含location對象,如果使用swith語句,可以把上面的代碼改寫為如下所示的代碼:

with (location) {

var gs=search.substring(1);

var hostname=hostname;

var url=href;

}

switch語句: 與C語言同,但是在JS中,可以在switch中使用任何資料類型。

 

函數

function functionName (arg0,arg1,...,argN) {  statements;}//e.g.function sayHi (name,message) { alert ("hello "+ name +","+ message);}

 可以向JS函數傳遞任意數量的參數,並且可以通過arguments對象來訪問這些參數。

由於不存在函數簽名的特性,JS函數不能重載。

 

JavaScript初學基本概念和文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.