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 concept of es6 arrowhead function

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "what is the concept of es6 arrow function". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the concept of es6 arrow function" can help you solve the problem.

In es6, the arrow function is a new syntax for defining functions using "= >"; the this in the body of the arrow function always points to the object in which it is defined, not to the object that calls it, and the syntax is "let fun= (parameters) = > {function body};".

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

What does the es6 arrow function mean?

The arrow function is one of the most interesting new features in ES6. As the name implies, the arrow function is a new syntax for defining functions using arrows (= >), but it is slightly different from the traditional JavaScript function, focusing on the following:

There are no this, super, arguments, and new.target bindings, and their values are determined by the non-arrow function at the nearest layer of the periphery

Cannot be called through the new keyword

There is no prototype.

Cannot change the binding of this

Arguments objects are not supported

Duplicate named parameters are not supported

The this in the body of a function always points to the object that defines it, not to the object that calls it. We know that the function in es5 executes it, and it points to it.

As follows:

Var f = v = > vrampac / equivalent to var f = function (v) {return v;} var sum = (num1,num2) = > num1+num2; / / equivalent to var sum = function (num1,num2) {return num1+num2} [1JI 2MJ 3] .map (function (x) {return x * x;}); / / Arrow function [1mai 2e 3] .map (x = > x * x); / / a lot of simplicity

We can see from the example that function is omitted and curly braces'{} 'are replaced by' = >'. This way of writing is more concise.

Examples are as follows

/ / 1. The default value of the parameter after / / es6 can only be taken in a disguised form: function test (afort b) {afora | | 2; breadb | | 2; return aquib} console.log (test ()); / / but this has some drawbacks. When the value we pass is 0, we will still use the default value. After the transformation, function test1 (a=typeof) {function axioms = "undefined"? 2Vera; b=typeof bads = "undefined"? 2VOB; return astatb} console.log (test1 (0mem0)) / / es6 provides us with default values / / grammatical sugars: function (function) {} let test2= (let test2= 2) = > {return aqb}; console.log (test2 ())

Output result:

This is the end of the introduction to "what is the concept of es6 Arrow function". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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