“块作用域构造let和const”的版本间的差异
来自CloudWiki
(创建页面,内容为“==let声明== ===原先的写法=== <nowiki>//函数内部 function changeState(flag){ if(flag){ var color = "red"; } else{ console.log(color); //此处可…”) |
(→全局块作用域绑定) |
||
(未显示同一用户的2个中间版本) | |||
第26行: | 第26行: | ||
===ES6新写法=== | ===ES6新写法=== | ||
+ | let声明的变量不会被提升,可以把变量的作用域限制在当前代码块中 | ||
+ | |||
<nowiki> | <nowiki> | ||
//函数内部 | //函数内部 | ||
第50行: | 第52行: | ||
console.log("i = " + i); //此处不能访问变量i,报错:i is not defined | console.log("i = " + i); //此处不能访问变量i,报错:i is not defined | ||
</nowiki> | </nowiki> | ||
+ | |||
+ | 使用let声明变量,还可以防止变量的重复声明: | ||
+ | |||
+ | <nowiki>var index = 0; | ||
+ | var index = 10; //OK | ||
+ | let index = 100; //报错:Identifier 'index' has already been declared */ | ||
+ | |||
+ | </nowiki> | ||
+ | |||
+ | 同一作用域霞,不能使用let1重复定义,但是不同的作用域下是可以的: | ||
+ | |||
+ | <nowiki>var index = 0; | ||
+ | { | ||
+ | let index = 10; //OK | ||
+ | }</nowiki> | ||
+ | ==const声明== | ||
+ | ES6 提供了const关键字,用于声明常量。 | ||
+ | |||
+ | 每个通过const关键字声明的常量必须在声明的同时进行初始化。 | ||
+ | |||
+ | <nowiki> | ||
+ | const sizeOfPage = 10; //正确 | ||
+ | |||
+ | const maxItems; //错误 | ||
+ | maxItems = 200;</nowiki> | ||
+ | |||
+ | 如果使用const声明对象,对象本身的绑定不能修改,但是对象的属性和值是可以修改的: | ||
+ | |||
+ | <nowiki> | ||
+ | const person = { | ||
+ | name: "zhangsan" | ||
+ | }; | ||
+ | |||
+ | person.name = "lisi"; //OK | ||
+ | person.age = 20; //OK | ||
+ | |||
+ | //错误,报错:Assignment to constant variable. | ||
+ | person = { | ||
+ | name: "wangwu" | ||
+ | };</nowiki> | ||
+ | |||
+ | ==全局块作用域绑定== | ||
+ | 在全局作用域中使用var声明的变量或对象,可能会无意中覆盖一个已经存在的全局属性: | ||
+ | |||
+ | <nowiki> | ||
+ | var greeting = "Welcome"; | ||
+ | console.log(window.greeting); // Welcome | ||
+ | |||
+ | console.log(window.Screen); // function Screen() { [native code] } | ||
+ | var Screen = "liquid crystal"; | ||
+ | console.log(window.Screen); // liquid crystal</nowiki> | ||
+ | |||
+ | 如果在全局作用域下使用let或const,则会在全局作用域下创建一个新的绑定,但该绑定不会成为window对象的属性: | ||
+ | |||
+ | <nowiki> | ||
+ | let greeting = "Welcome"; | ||
+ | console.log(window.greeting); // undefined | ||
+ | |||
+ | const Screen = "liquid crystal"; | ||
+ | console.log(Screen === window.Screen); // false</nowiki> |
2021年1月28日 (四) 06:48的最新版本
let声明
原先的写法
//函数内部 function changeState(flag){ if(flag){ var color = "red"; } else{ console.log(color); //此处可以访问变量color,其值为:undefined return null; } } changeState(false); //块中 { var a = 1; } console.log("a = " + a); //此处可以访问变量a,输出:a = 1 //for循环中 for(var i = 0; i < 10; i++){ } console.log("i = " + i); //此处可以访问变量i,输出:i = 10*/
ES6新写法
let声明的变量不会被提升,可以把变量的作用域限制在当前代码块中
//函数内部 function changeState(flag){ if(flag){ let color = "red"; } else{ console.log(color); //此处不能访问color,报错:color is not defined return null; } } changeState(false); //块中 { let a = 1; } console.log("a = " + a); //此处不能访问变量a,报错:a is not defined //for循环中 for(let i = 0; i < 10; i++){ } console.log("i = " + i); //此处不能访问变量i,报错:i is not defined
使用let声明变量,还可以防止变量的重复声明:
var index = 0; var index = 10; //OK let index = 100; //报错:Identifier 'index' has already been declared */
同一作用域霞,不能使用let1重复定义,但是不同的作用域下是可以的:
var index = 0; { let index = 10; //OK }
const声明
ES6 提供了const关键字,用于声明常量。
每个通过const关键字声明的常量必须在声明的同时进行初始化。
const sizeOfPage = 10; //正确 const maxItems; //错误 maxItems = 200;
如果使用const声明对象,对象本身的绑定不能修改,但是对象的属性和值是可以修改的:
const person = { name: "zhangsan" }; person.name = "lisi"; //OK person.age = 20; //OK //错误,报错:Assignment to constant variable. person = { name: "wangwu" };
全局块作用域绑定
在全局作用域中使用var声明的变量或对象,可能会无意中覆盖一个已经存在的全局属性:
var greeting = "Welcome"; console.log(window.greeting); // Welcome console.log(window.Screen); // function Screen() { [native code] } var Screen = "liquid crystal"; console.log(window.Screen); // liquid crystal
如果在全局作用域下使用let或const,则会在全局作用域下创建一个新的绑定,但该绑定不会成为window对象的属性:
let greeting = "Welcome"; console.log(window.greeting); // undefined const Screen = "liquid crystal"; console.log(Screen === window.Screen); // false