当前位置:网站首页>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对象。(这句话是抄来的,暂时没有这么用过,仅供参考)
边栏推荐
- IO playback function of FIO
- Typescript -- Section 5: classes
- TypeScript--第五节:类
- 在线买股票开户安全嘛?
- Typescript -- Section 1: basic types
- Windows10 phpstudy installing redis extension
- 单机多实例MYSQL主从复制
- [Electronic Experiment 2] simple electronic doorbell
- PHP uses endroid/qrcode QR code to generate, and Gd library generates sharing posters
- Stm32f407-------- NVIC interrupt priority management
猜你喜欢

What pitfalls should be avoided in the job interview for the operation post in 2022?

MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle

Along with the notes: methods simulating array like classes

TypeScript -- 第二节:变量声明

Quartz explanation and use

What are some tips to improve your interview success rate?

mysql 8.0以上报2058 解决方式
![SQL note 2 [MySQL]](/img/a4/f711173ce731d95860746e309b7d74.jpg)
SQL note 2 [MySQL]

ctfshow XSS

MapReduce case
随机推荐
Notes: three ways to define setters and Getters
Zoom with mouse wheel
The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (III)
ERROR 1067 (42000): Invalid default value for ‘end_ time‘ Mysql
"Five considerations" for safe use of the Internet
Reading notes of English grammar new thinking Basic Edition 2 (I)
stm32F407-------跑马灯、蜂鸣器
Use menu resources to implement option menus and context menus
Stm32f407------- external interrupt
TypeScript -- 第一节:基础类型
PHP uses endroid/qrcode QR code to generate, and Gd library generates sharing posters
Mysql-5.7.30-winx64 installation free download and installation tutorial
LinkedIn DataHub --- 经验分享
Quartz explanation and use
Easy to use free ppt template
Scrapy使用xlwt实现将数据以Excel格式导出的Exporter
Behaviortree in ros2
pymysql. Error get error code and specific error information
Stm32f407 ------ serial (serial port) communication
Trois questions PWN