当前位置:网站首页>Arrow function of new features of ES6
Arrow function of new features of ES6
2022-07-29 03:45:00 【Haha, APE】
summary :ES6 Arrows are allowed (=>) Defined function , Arrow functions provide a more concise way to write functions , Arrow functions are often used to define anonymous functions ;
Note on arrow function
- If there is only one parameter , Then brackets can be omitted
- If there is only one statement in the function body , The curly braces can be omitted
- Arrowhead function this Refers to the this value
- Arrow functions cannot be instantiated as constructors
- Out of commission arguments
The properties of the arrow function
- Arrowhead function this Is static , Always point to this value
- Cannot instantiate an object as a construct
- Out of commission arguments Variable
Code demonstration
The traditional way of writing
var sayHi = function(){
console.log(" hello ! Xiao Liu ");
}
sayHi()
// Running results : hello ! Xiao Liu
ES6 How to write it : No parameter
let speak = ()=>console.log("hello ha-ha !");
speak()
// Running results :hello ha-ha !
The traditional way of writing One parameter
var hello = function(name){
return ` hello !${
name}`
}
console.log(hello(" Xiao Liu "));
// Running results : hello ! Xiao Liu
ES6 Arrow function : One parameter
let Hi = name => `hello${
name}`
console.log(Hi(" Xiao Liu "));
// Running results :hello Xiao Liu
The traditional way of writing : Multiple parameters
var sum = function(a,b,c){
return a+b+c
}
console.log(sum(1,2,3));
// Running results :6
ES6 How to write it Multiple parameters
let add = (a,b,c)=>a + b + c
console.log(add(6,7,8));
// Running results :21
Feature case demonstration
Arrowhead function this Is static , Always point to... Under the scope where the function is declared this Value
const school = {
call:" hello !",
name:" Xiao Liu "
}
// Traditional functions
function sayHiName(){
console.log(this.call+this.name);
}
// Arrow function
let sayHiName1 = ()=>{
console.log(this.call+this.name);}
window.name = " Xiao Zhang "
// Call directly
sayHiName()
// undefined Xiao Zhang
sayHiName1()
// undefined Xiao Zhang
Use call call
sayHiName.call(school)
// hello ! Xiao Liu
sayHiName1.call(school)
// undefined Xiao Zhang
// summary : Arrowhead function this Is static , Always point to... Under the scope where the function is declared this Value
Cannot instantiate an object as a construct
let Person = (name,age) =>{
this.name = name
this.age = age
}
let me = new Person(" Xiao Zhang ",24)
console.log(me);
// Report errors :t TypeError: Person is not a constructor
Out of commission arguments Variable
let fn = () =>console.log(argument);
fn(1,2,3)
// Report errors argument is not defined
边栏推荐
- 内连接和左连接简单案例
- 实例搭建Flask服务(简易版)
- Install the packet capturing certificate
- 座机的表单验证
- Instance setup flask service (simple version)
- In depth C language (4) -- definition and use of switch
- 力扣每日一题-第44天-205. 同构字符串
- Violence recursion to dynamic programming 01 (robot movement)
- 1985-2020 (8 Editions) global surface coverage download and introduction
- (codeforce547) c-mike and foam
猜你喜欢

AI_ Drug: VAE of molecular generation model (I)

How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency

(2022 Hangdian multi school III) 1002 boss rush (pressure dp+ dichotomy)

Solve the problem of garbled code when opening the project code in idea

Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators

Overestimated test driven development?
![Machine learning [numpy]](/img/6b/3e7f08c5d379ce35687e4f14545929.png)
Machine learning [numpy]

Shutter start white screen

CUB_200鸟类数据集关键点可视化

Deep into C language (1) -- operators and expressions
随机推荐
向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
安装抓包证书
Violence recursion to dynamic programming 01 (robot movement)
three. JS Part 54 how to pass structure array to shader
Li Kou daily question - day 44 -205. Isomorphic string
(codeforce547)C-Mike and Foam(质因子+容斥原理)
MOS tube - rapid recovery application notes (II) [parameters and applications]
通过递归实现多级联动
消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!
Practical application cases of digital Twins - smart energy
向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
内连接和左连接简单案例
(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
Uni app internationalization
路西法98-生活记录ing
Simple code implementation of K-means clustering
Simple understanding of CDN, SDN and QoS
Deep into C language (1) -- operators and expressions
机器学习【Numpy】