在Javascript中 聲明時用"var"與不用"var"的區別,== 和 ===的區別

來源:互聯網
上載者:User

標籤:java

1. 在Javascript中 聲明時用"var"與不用"var"的區別

Javascript聲明變數時

var a = 111;

a = 111;

兩種方式一樣嗎?

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

var a = 11;function test4(){    var a = 22;}test4();console.log(a);

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

結果是什麼呢? 11

這個好理解, 函數內的var a聲明是內部變數,這時結果是第一個a的值.

變動一下如下:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

var abc = 11;function test4(){    abc = 22;}test4();console.log(abc);

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

結果是什麼呢? 22

再改:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

function test4(){    var aaa = 22;}test4();console.log(aaa);

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

結果怎樣? 運行報錯了! ReferenceError: aaa is not defined!

改:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

function test4(){    var aaa = 22;}test4();console.log(test4.aaa);

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

運行不會報錯,輸出結果是 undefined.

函數或者物件建構內聲明的變數是私人的. 外部無法訪問到. 包括原型繼承後的對象. 

可是如果這樣:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

function test4(){    bbb = 33;}test4();console.log(bbb);

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

結果是 33

 

這就是有var 和沒有 var的聲明的區別:

 

很可怕假如一個大的項目,在這裡改變了bbb的值, 並沒有添加var 碰巧整個項目全域變數有個同名bbb被改變,不加var不是只作用在這個函數或對象內. 出了錯誤很難找.

所以書寫代碼必須謹慎. 聲明變數改加的就加不能怕麻煩. 結果是完全不同的.

 

結論: (1).在函數範圍內 加var定義的變數是局部變數,不加var定義的就成了全域變數。

使用var定義

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

var a = ‘hello World‘;function bb(){    var a = ‘hello Bill‘;    console.log(a);   }bb()   // ‘hello Bill‘console.log(a);    // ‘hello world‘

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

不使用var定義

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

var e = ‘hello world‘;function cc(){    e = ‘hello Bill‘;    console.log(e);    // ‘hello Bill‘}cc()   // ‘hello Bill‘console.log(e)     // ‘hello Bill‘

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

(2).在全域範圍下,使用var定義的變數不可以delete,沒有var定義的變數可以delete.也就說明隱含全域變數嚴格來說不是真正的變數,而是全域對象的屬性,因為屬性可以通過delete刪除,而變數不可以。

 

(3).使用var 定義變數還會提升變數聲明,即

使用var定義:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

function hh(){    console.log(a);    var a = ‘hello world‘;}hh()    //undefined

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

不使用var定義:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

function hh(){    console.log(a);    a = ‘hello world‘;}hh()    // ‘a is not defined‘

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

這就是使用var定義的變數的聲明前提。

 

 

2. JS中的!=、== 、!==、===的用法和區別。

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

var num = 1; var str = ‘1‘; var test = 1; test == num   //true 相同類型 相同值 test === num  //true 相同類型 相同值 test !== num  //false test與num類型相同,其值也相同, 非運算肯定是false  num == str   //true  把str轉換為數字,檢查其是否相等。 num != str   //false  == 的 非運算 num === str  //false  類型不同,直接返回false num !== str  //true   num 與 str類型不同 意味著其兩者不等 非運算自然是true啦

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="font-size:inherit;line-height:inherit;margin:0px;padding:0px;vertical-align:middle;border:medium none;background-repeat:no-repeat;" />

(1). JavaScript"=="的作用

       == 和 != 比較若類型不同,先償試轉換類型,再作值比較,最後傳回值比較結果。

  1. 當==兩邊的內容是字串時,則比較字串的內容是否相等。

  2. 當==兩邊的內容是數字時,則比較數位大小是否相等。

  3. 當==兩邊的內容是對象或者是對象的函數屬性時,則比較記憶體位址是否相等。

(2). ==和===的區別

      === 和 !== 只有在相同類型下,才會比較其值。

  ==用於一般比較,===用於嚴格比較,==在比較的時候可以轉換資料類型,===嚴格比較,只要類型不符就返回flase。

總結:

  ==和===的區別:"==" 只要求值相等; "===" 要求值和類型都相等


在Javascript中 聲明時用"var"與不用"var"的區別,== 和 ===的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.