亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談JavaScript中定義變量時有無var聲明的區(qū)別

 更新時間:2014年08月18日 15:41:36   投稿:hebedich  
這篇文章主要介紹了JavaScript中定義變量時有無var聲明的區(qū)別分析以及示例分享,需要的朋友可以參考下

前段時間回答了一個關于定義變量時使用關鍵字var與否的區(qū)別,總結回顧一下。

1.在函數(shù)作用域內 加var定義的變量是局部變量,不加var定義的就成了全局變量。
使用var定義:

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

不使用var定義:

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

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

3.使用var 定義變量還會提升變量聲明,即
使用var定義:

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

不使用var定義:

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

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

4.在ES5的'use strict'模式下,如果變量沒有使用var定義,就會報錯。

相關文章

最新評論