MoonCheung

MoonCheung

人生路漫漫,何曾有坦途。:)

Arrow functions and regular functions differ in the following ways:

  1. About arrow parameters:
    1. If an arrow function has no parameters, simply write an empty parentheses.
    2. If an arrow function has only one parameter, it needs to be wrapped in parentheses.
    3. If an arrow function has multiple parameters, separate them with commas and wrap them in parentheses.
  2. About arrow function body:
    1. Arrow functions do not use the "function" keyword.
    2. 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.
    3. If an arrow function body consists of only one line of code that returns an object, the object needs to be wrapped in parentheses.
    4. 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.
  3. The difference is:
    1. Essence of arrow functions:
      1. More concise and clear syntax.
      2. Arrow functions are anonymous functions.
      3. 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.
    2. Essence of regular functions:
      1. 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.
      2. Regular functions create their own "this" execution context in their scope chain, and the priority is: new > bind > obj > window.
      3. Multiple functions can be declared, such as function expressions, function declarations, and anonymous functions.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.