Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the arrow function in ES5

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what the arrow function in ES5 is, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

What is the arrowhead function?

Arrow function expressions have a more concise syntax than function expressions and do not have their own this,arguments,super or new.target. The arrow function expression is more suitable for places where anonymous functions are needed, and it cannot be used as a constructor.

/ / ES5 Versionvar getCurrentDate = function () {return new Date ();} / ES6 Versionconst getCurrentDate = () = > new Date ()

In this example, the ES5 version has the function () {} declaration and the return keyword, which are required to create the function and return the value, respectively. In the arrow function version, we only need () parentheses, not the return statement, because if we only have one expression or value to return, the arrow function will have an implicit return.

/ / ES5 Versionfunction greet (name) {return 'Hello' + name +'!;} / / ES6 Versionconst greet = (name) = > `Hello ${name} `; const greet2 = name = > `Hello ${name}`

We can also use the same parameters as function expressions and function declarations in arrow functions. If we have an argument in an arrow function, we can omit the parentheses.

Const getArgs = () = > argumentsconst getArgs2 = (... rest) = > rest

The arrow function cannot access the arguments object. So calling the first getArgs function throws an error. Instead, we can use the rest parameter to get all the parameters passed in the arrow function.

Const data = {result: 0, nums: [1,2,3,4,5], computeResult () {/ / where "this" refers to the "data" object const addAll = () = > {return this.nums.reduce ((total, cur) = > total + cur, 0)}; this.result = addAll ();}}

The arrow function does not have its own this value. It captures the this value of the lexical scope function, which in this example will copy the this value in the computeResult method, which is the window object if we declare the arrow function in the global scope.

Thank you for reading this article carefully. I hope the article "what is the Arrow function in ES5" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report