- About arrow parameters:
- If an arrow function has no parameters, simply write an empty parentheses.
- If an arrow function has only one parameter, it needs to be wrapped in parentheses.
- If an arrow function has multiple parameters, separate them with commas and wrap them in parentheses.
- About arrow function body:
- Arrow functions do not use the "function" keyword.
- If an arrow function body consists of only one line of code, which is a simple return statement of a variable or a simple JavaScript expression, the curly braces {} can be omitted.
- If an arrow function body consists of only one line of code that returns an object, the object needs to be wrapped in parentheses.
- If an arrow function body consists of only one statement and does not need to return a value (commonly used for calling a function), the statement can be preceded by the "void" keyword.
- The difference is:
- Essence of arrow functions:
- More concise and clear syntax.
- Arrow functions are anonymous functions.
- Arrow functions do not create their own "this" context; they inherit "this" from the outer scope. This is because the reference of "this" in an arrow function is determined when it is defined and cannot be changed.
- Essence of regular functions:
- The essence of "new" is to create a new object, set its "proto" (prototype chain) to the function's prototype, execute the "call" method, and finally assign the new object to the declared variable.
- Regular functions create their own "this" execution context in their scope chain, and the priority is: new > bind > obj > window.
- Multiple functions can be declared, such as function expressions, function declarations, and anonymous functions.
- Essence of arrow functions: