1. Use of arrow functions ES6 adds a new method of defining function expressions using arrows The second part of this article will introduce the functional differences between arrow functions and ordinary functions. 1. From normal function to arrow function As shown below, to define a function, you can use function getSum(a, b) { return a + b; } If you use an arrow function to define this function, you can write it as follows, omitting the const getSum = (a, b) => { return a + b; }; 2. Omit curly braces and return If you define an arrow function and there is only a As shown below, the complete definition of the arrow function. const getSum = (a, b) => { return a + b; }; This arrow function has only a const getSum = (a, b) => a + b; 3. Omit parenthesesIf the defined arrow function has only one parameter, the parentheses can be omitted. As shown below, the defined arrow function has only one parameter and is written in full. const func = (a) => { return a + 2; }; The parentheses around the parameters are omitted, and the following code is equivalent to the above code. const func = a => a + 2; Note : If the function has no parameters, or has multiple parameters, parentheses must be used. 2. The difference between arrow functions and normal functionsIn most cases, arrow functions can be used wherever ordinary functions can be used, because arrow functions are more concise. But in some cases, arrow functions are very different from ordinary functions. 1. The this of the arrow function is the this of the parent scope The following code defines an object const obj = { name: 'Jack', getThis() { console.log(this); }, }; obj.getThis(); // {name: 'Jack', getThis: ƒ} Similarly, an object const obj = { name: 'Jack', getThis: () => { console.log(this); }, }; obj.getThis(); // Window 2. call, apply, and bind cannot change the this of arrow functions In the following code, the ordinary function const obj = { name: 'Jack', }; function getName() { console.log(this.name); } getName.call(obj); // Jack If the function is changed to an arrow function, const obj = { name: 'Jack', }; const getName = () => { console.log(this.name); }; getName.call(obj); // undefined 3. Cannot be used as a constructorArrow functions cannot be used as constructors. If you use an arrow function as a constructor, an error will be reported, as shown in the following code. const Person = () => { this.name = 'Jack'; }; const usr = new Person(); // Person is not a constructor 4. No arguments Inside a normal function, you can use function func() { console.log(arguments); } func(1, 2, 3); // [Arguments] { '0': 1, '1': 2, '2': 3 } However, arrow functions cannot use the In the browser, if you use const func = () => { // Browser environment console.log(arguments); // arguments is not defined }; func(1, 2, 3); However, the arrow function can obtain the input parameters in the form of const func = (...args) => { console.log(args); // [ 1, 2, 3 ] }; func(1, 2, 3); 5. Arrow functions do not support new.target Inside the constructor of a normal function definition, function Person() { this.name = 'Jack'; console.log(new.target); } // Points to the constructor new Person(); // [Function: Person] In arrow functions, const Person = () => { this.name = 'Jack'; console.log(new.target); } // Browser environment new Person(); // new.target expression is not allowed here
This concludes this article about the characteristics of JavaScript arrow functions and the differences from ordinary functions. For more information about js arrow functions and ordinary functions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Two ways to implement square div using CSS
Recently, when using kazam in Ubuntu 20.04 for re...
Table of contents Preface 1. Download a single fi...
This article shares the specific code of canvas t...
1. Command Introduction The date command is used ...
Required effect: After clicking to send the verif...
A Thorough Analysis of HTML (14) Special Characte...
wedge Because the MySQL version installed on the ...
If you don’t understand what I wrote, there may b...
Table of contents 1. Node builds HTTP server 2. H...
Storage Engine What is a database storage engine?...
What is Vite? (It’s a new toy on the front end) V...
Docker is an open source engine that makes it eas...
1. Unzip nginx-1.8.1.tar.gz 2. Unzip fastdfs-ngin...
1. Basics of Linux Firewall The Linux firewall sy...
This article example shares the specific implemen...