Home >Web Front-end >JS Tutorial >Discussion about scope in JavaScript ES6

Discussion about scope in JavaScript ES6

巴扎黑
巴扎黑Original
2017-07-22 17:09:021409browse

ES6 scope(scope)

Scope:
1.Global scope(global) 2.Function scope(function)

Global scope

var a=1;
console.log(a);//1
    //{}表示语句块
    
if(a==1){
    var b=2;
    console.log(b);//2
}
console.log(b);

for (var c = 0; c < 10; c++) {
    ;
};
    console.log(c);

function fn(){
    var d=3;
    console.log(d);
}
fn();
//console.log(d);//报错

console.log("---window---");
console.log(a);//1
console.log(b);//2
console.log(c);//10
console.log(d);//报错

//全局变量 挂载在window对象的属性。

//声明变量有前置功能(hosting hot)

//函数也有前置功能

console.log(c);
var c;//undefined

Function scope

var a=1;
function fn(){
    console.log(a);//1
    /*console.log(a);//undefined
    var a=2;*/
}
fn();

The above is the detailed content of Discussion about scope in JavaScript ES6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn