当前位置:网站首页>The difference between arrow function and ordinary function

The difference between arrow function and ordinary function

2022-06-11 06:57:00 Xiaoman's code world

Reference resources :https://juejin.cn/post/7050492355056664612

The arrow function doesn't [[Construct]]

  1. The function object is a support [[Call]] 、 [[Construct]] Objects of internal methods . Every support [[Construct]] Object of must support [[Call]], in other words , Each constructor must be a function object . therefore , Constructors can also be constructor functions or constructor objects .
    therefore , Want to use... For an object new, You have to make sure that the object has [[Construct]] This internal approach . The arrow function does not [[Construct]].

Why does the arrow function not [[Construct]]

Arrow function at the beginning of design , To design a shorter function without binding this. So the arrow function is not a parameter super、this、new.target Define local binding . In the arrow function this、super、arguments And new.target These values are determined by the nearest non arrow function on the periphery .
The designer of such a function does not think it is necessary to delegate to a function that can [[Construct]] The objective function of , So the arrow function doesn't [[Construct]].
At the same time, there is no need to construct prototypes , So the arrow function doesn't exist prototype attribute .

The difference between arrow function and ordinary function

  1. No, this、super、arguments And new.target binding :this、super、arguments And new.target The value of is determined by the nearest non arrow function .
  2. Can not be new call : The arrow function doesn't [[Construct]] Method , Therefore, it cannot be used as a constructor , Use new Calling the arrow function will throw an error .
  3. There is no prototype : Since you can't use the arrow function new, Then he doesn't need a prototype , There is no prototype attribute .
  4. Can't change this:this The value of cannot be modified inside the function , The value of a function remains constant throughout its life cycle .
  5. No, arguments object : The arrow function doesn't arguments binding , You must rely on named or remaining arguments to access the parameters of a function .
  6. Duplicate named parameters are not allowed : Arrow functions are not allowed to have duplicate named arguments , Whether or not in strict mode : And relatively speaking , Traditional functions prohibit this repetition only in strict mode .
     Insert picture description here
     Insert picture description here
原网站

版权声明
本文为[Xiaoman's code world]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020525053348.html