当前位置:网站首页>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
边栏推荐
- KNN method predicts pregnancy, KNN principle simple code
- RTP send and receive h265
- Alibaba Font Icon Library Usage and update methods
- Sleuth+Zipkin 来进行分布式服务链路的追踪
- Some notes on uniapp
- (2022 Hangdian multi school III) 1011 link is as bear (thinking + linear basis)
- How fast does it take to implement a super simple programming language?
- JS regular expression finds the number of times a character (string) appears (one line of code)
- Easy to use remote sensing data set download website~~~
- Instance setup flask service (simple version)
猜你喜欢

Analysis of new retail o2o e-commerce model

Microcomputer principle and interface technology

实例搭建Flask服务(简易版)

内连接和左连接简单案例

深入C语言(1)——操作符与表达式

I.MX6U-驱动开发-2-LED驱动

Flutter 启动白屏

Why is continuous integration and deployment important in development?

CUB_ Visualization of key points in 200 bird dataset

for_ Example of each usage
随机推荐
How to understand "page storage management scheme"
RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密
深入C语言(4)——switch的定义与使用
Spark dataframe replaces empty characters (or other values) in each column with null
座机的表单验证
AI_ Drug: VAE of molecular generation model (I)
The difference between int and integer. Is int or integer used in practical applications?
机器学习【Numpy】
SFTP upload error: com.jcraft.jsch JSchException: connection is closed by foreign host
Ribbon principle analysis namedcontextfactory
(2022杭电多校三)1011-Link is as bear(思维+线性基)
Build redis environment under windows and Linux
Deep understanding of Base64 underlying principles
大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
@Configuration (proxybeanmethods = false) what's the use of setting this to false
(nowcoder22529c) diner (inclusion exclusion principle + permutation and combination)
深入C语言(2)——结构的定义与使用
1. 头文件-注释-命名空间-标准输入输出流
Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率
RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption