当前位置:网站首页>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
边栏推荐
- Set functions in kotlin
- Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
- Why do programmers so "dislike" the trunk development mode?
- 1. Header file - Comment - namespace - standard input / output stream
- Sleuth+Zipkin 来进行分布式服务链路的追踪
- 1.6 example: cifar-10 classification
- Casbin入门
- KNN method predicts pregnancy, KNN principle simple code
- How to judge stun protocol
- How fast does it take to implement a super simple programming language?
猜你喜欢

EMD empirical mode decomposition

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

1. Mx6u driver development-2-led driver

Solve the delay in opening the console of Google browser

Vs code must know and know 20 shortcut keys!

深入C语言(3)—— C的输入输出流

通过递归实现多级联动

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

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

新零售O2O 电商模式解析
随机推荐
数字孪生实际应用案例-智慧能源篇
深入C语言(1)——操作符与表达式
CUB_ Visualization of key points in 200 bird dataset
Complexity analysis learning
第一个ALV程序2
Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
for_ Example of each usage
Flutter 启动白屏
Sleuth+zipkin to track distributed service links
Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率
Casbin入门
(nowcoder22529C)dinner(容斥原理+排列组合)
Typescript from entry to mastery (XVIII) joint type and type protection
【C语言入门】ZZULIOJ 1031-1035
深入C语言(4)——switch的定义与使用
The latest second edition of comic novels, listening to books, three in one, complete source code / integrated visa free interface / building tutorials / with acquisition interface
《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
Solve the delay in opening the console of Google browser
for_each用法示例