Home > Web Front-end > JS Tutorial > body text

Discussion about scope in JavaScript ES6

巴扎黑
Release: 2017-07-22 17:09:02
Original
1338 people have browsed it

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
Copy after login

Function scope

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!