当前位置:网站首页>ES6:let、const、箭头函数
ES6:let、const、箭头函数
2022-06-28 23:59:00 【德克萨斯的松鼠】
ES6中的let与const
目前常用的几种定义方式是有以下的三种:
var a = 100;
let b = 99;
const c = 98;
var、let / const的区别
如果我们在控制台中直接输入以下两行代码,所出现的结果是不同的
console.log(y)
var/let/const y = 100
var
js中有预解析功能,它会提前的解析 var 和 function。就以上两行代码而言,如果用var去定义的话,解析顺序应当如下:
var y; // 预解析不代表预赋值
console.log(y);
y = 100;
如此结果为undefined
注意:函数表达式 和 函数声明 的区别就在这里。由于function会被提前解析,所以该函数会被优先调用
// 函数声明
function f2(){
return 123;
}
// 函数表达式
var f = function(){
return 123;
}
let、const
let 与 const都不存在变量提升一说,所以很显然,如果将以上两行代码中var换为let 或 const就会报错。与var不同的,let在同一个作用域下不能重复定义同一个名称。
var、let / const的作用域
var为函数作用域------若定义在某一函数中时,那么就只能在该函数中使用。
let/const为块级作用域------只能在它所在的大括号中使用.例如:
function f(){
let/const a = 100;
if(true){
let/const a = 1000;
};
console.log(a);
};
f();
在控制台输出结果为100(let的两个a不为同一个),把let换为var则为1000。
let与const的差别
const只能声明一个可读的常量(不能只定义不赋值)例如:
function f(){
const a = 100;
if(true){
const a = 1000;
};
console.log(a);
};
f();
此时的值同let一样也为100,但是,如果把if执行语句中的const a = 1000;改为a = 1000,则会报错,而let定义则不会,因为const定义的常量不能更改。
注意:const声明的常量一旦声明必须初始化且不可更改。但声明的引用类型,对应的值是可以修改的。 例如:
const obj = {
}
obj.name = 'amy'
console.log(obj)
const arr = []
arr.push(1)
是没有问题的,代码中const所声明的引用类型仅表示储存位置不会改变
ES6中的箭头函数
箭头函数用法—let 函数名称 = (参数) = >函数体
箭头函数旨在简化函数
//正常写法
var f = function(n1,n2){
return n1 + n2;
}
//箭头函数写法
let f = (n1,n2) =>n1 + n2; //当只有一个参数时括号可以省去。没有参数时应当写一个空括号
箭头函数的this指向
箭头函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
var name = '张三';
var person = {
name:'李四'
test:function(){
console.log(this.name) //李四
}
}
person.test();
var person2 = {
name:'李四'
test: ()=>{
console.log(this,name); // 张三,当前this指向了定义时所在的对象(window)
}
}
person2.test();
并不是所有的函数都适合改为箭头函数。当函数中使用本身的 this 时,我们再用箭头函数将会造成冲突,此时应在函数外面定义一个变量 let $this = this 。在函数内部通过 $this来操作全局vue对象。(这句话是抄来的,暂时没有这么用过,仅供参考)
边栏推荐
- TypeScript --第三节:接口
- MapReduce案例
- Baidu knows the crawler, and obtains the dialogue below the comment according to the question Id, clue ID and comment ID
- Learn binary tree like this
- 好用免费的PPT模板
- stm32F407-------电容触摸按键
- With notes: re understanding else if
- pymysql. Error get error code and specific error information
- 随笔记:定义setter和getter的三种方式
- Stm32f407 ------ serial (serial port) communication
猜你喜欢
随机推荐
mysql 8.0以上报2058 解决方式
Leetcode 178 Score ranking (June 27, 2022)
Typescript -- Section 5: classes
SQL note 2 [MySQL]
stm32F407-------IO引脚复用映射
The secondary market is full of bad news. How should the market go next? One article will show you the general trend
一条update语句到底加了多少锁?带你深入理解底层原理
[SSM] an error is reported that the user name of the access denied for user 'WYF' @ 'localhost' (using password: yes) data becomes the user name of the computer
What are some tips to improve your interview success rate?
从SQL注入绕过最新安全狗WAF中学习fuzz
TypeScript -- 第二节:变量声明
Stm32f407----- capacitive touch button
12.物體檢測Mask-Rcnn
11.目标分割
stm32F407-------LCD
TypeScript -- 第七节 枚举
Stm32f407 ------- GPIO input experiment
[buuctf.reverse] 131-135
Xiaobai's e-commerce business is very important to choose the right mall system!
MapReduce案例





![[buuctf.reverse] 131-135](/img/c2/b8b06c8191af2c75bf4ad5c82feaea.png)


