当前位置:网站首页>Es6:let, const, arrow functions
Es6:let, const, arrow functions
2022-06-29 00:05:00 【Texas squirrel】
ES6 Medium let And const
At present, there are three commonly used definitions :
var a = 100;
let b = 99;
const c = 98;
var、let / const The difference between
If we enter the following two lines of code directly in the console , The results are different
console.log(y)
var/let/const y = 100
var
js There is a pre parsing function in , It will Advanced parsing var and function. As far as the above two lines of code are concerned , If you use var To define , The parsing order should be as follows :
var y; // Pre parsing does not represent pre assignment
console.log(y);
y = 100;
The result is undefined
Be careful : Function expression and Function declaration Here's the difference . because function Will be parsed in advance , So this function will be called first
// Function declaration
function f2(){
return 123;
}
// Function expression
var f = function(){
return 123;
}
let、const
let And const There is no such thing as variable promotion , So obviously , If the above two lines of code var Replace with let or const You're going to report a mistake . And var Different ,let You cannot define the same name repeatedly under the same scope .
var、let / const Scope of action
var Is the function scope ------ If defined in a function , Then it can only be used in this function .
let/const Is a block level scope ------ It can only be used in the braces in which it is located . for example :
function f(){
let/const a = 100;
if(true){
let/const a = 1000;
};
console.log(a);
};
f();
The output result on the console is 100(let Of the two a Not the same ), hold let Replace with var Then for 1000.
let And const The difference between
const Only one readable constant can be declared ( You can't just define unassigned ) for example :
function f(){
const a = 100;
if(true){
const a = 1000;
};
console.log(a);
};
f();
The value at this time is the same as let The same for 100, however , If you put if Execute... In the statement const a = 1000; Change it to a = 1000, May be an error , and let Definition does not , because const The definition of a constant cannot be changed .
Be careful :const Declared constants must be initialized and unchangeable once declared . But the declared reference type , The corresponding value can be modified . for example :
const obj = {
}
obj.name = 'amy'
console.log(obj)
const arr = []
arr.push(1)
There is no problem , In the code const The declared reference type only means that the storage location will not change
ES6 Arrow function in
Arrow function usage —let The name of the function = ( Parameters ) = > The body of the function
The arrow function is intended to simplify the function
// Normal writing
var f = function(n1,n2){
return n1 + n2;
}
// Arrow function writing
let f = (n1,n2) =>n1 + n2; // Parentheses can be omitted when there is only one parameter . You should write an empty parenthesis when there are no parameters
Arrowhead function this Point to
Inside the arrow function this object , Is the object of definition , Instead of using the object .
var name = ' Zhang San ';
var person = {
name:' Li Si '
test:function(){
console.log(this.name) // Li Si
}
}
person.test();
var person2 = {
name:' Li Si '
test: ()=>{
console.log(this,name); // Zhang San , At present this It points to the object of definition (window)
}
}
person2.test();
Not all functions are suitable for changing to arrow functions . When a function uses its own this when , If we use the arrow function again, it will cause conflicts , In this case, a variable should be defined outside the function let $this = this . Pass through... Inside the function $this To operate the global vue object .( This sentence is copied , I haven't used it for the time being , For reference only )
边栏推荐
- stm32F407-------外部中断
- What are some tips to improve your interview success rate?
- [C Prime plus chapitre II Questions de programmation après la Classe]
- Typescript-- section 4: Functions
- 11. target segmentation
- 随笔记:定义setter和getter的三种方式
- Windows平台下安装MySQL(附:Navicat Premium 12 “使用” 教程)
- [buuctf.reverse] 131-135
- MSYQL is abnormal. Don't worry. Mr. Allen will point out the puzzle
- Stm32f407------- external interrupt
猜你喜欢

三個pwn題

随笔记:模拟类数组(array-like)的方法

TypeScript -- 第二节:变量声明

Three PWN questions
JVM底层又是如何实现synchronized的
![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)
Edge extraction based on Halcon learning [2] circles Hdev routine

Phoenix installation tutorial

一条update语句到底加了多少锁?带你深入理解底层原理

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)
![[Electronic Experiment 2] simple electronic doorbell](/img/40/227f9ac1f427c1435e0e3aa02640b1.png)
[Electronic Experiment 2] simple electronic doorbell
随机推荐
Stm32f407 ------ running lamp and buzzer
6.28 learning content
Technology sharing | software development process that you must understand if you want to get started with testing
Stm32f407----- capacitive touch button
This thing is called a jump watch?
好用免费的PPT模板
The secondary market is full of bad news. How should the market go next? One article will show you the general trend
stm32F407-------跑马灯、蜂鸣器
LinkedIn datahub - experience sharing
TypeScript -- 第一节:基础类型
stm32F407-------通用定时器
[C Primer Plus Chapter II after class programming questions]
炒股开户万一免五是靠谱么,安全么
在线买股票开户安全嘛?
随笔记:定义setter和getter的三种方式
ES6:let、const、箭头函数
Is it safe and reliable to open a securities account in Yixue school?
【LeetCode】21. Merge two ordered linked lists - go language solution
MapReduce案例
stm32F407-------电容触摸按键