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
1.Jenkins installation steps: https://www.jb51.ne...
1. Install nginx in docker: It is very simple to ...
Mongodb has a db.serverStatus() command, which ca...
Official documentation: JSON Functions Name Descr...
Introduction The meta tag is an auxiliary tag in ...
1.1 General marking A general tag consists of an ...
Table of contents What is a Binary Search Tree (B...
Preface BINARY and VARBINARY are somewhat similar...
Table of contents 1. The role of array: 2. Defini...
A few days ago, I introduced to you a domestic xh...
1. Introduction pt-query-digest is a tool for ana...
Scenario Description In a certain system, the fun...
MAC installs mysql8.0, the specific contents are ...
Table of contents Short Polling Long-Polling WebS...
Table of contents 1. Location Object 1. URL 2. Pr...