This article brings the content is about the JS in the arrow function format &this and the common function of the difference between the explanation, there is a certain reference value, the need for friends can refer to, I hope to help you.
The format of the arrow function
Es5var selected = Alljobs.filter (function (Job) { return job.isselected ();}); /ES6 arrow function var selected = alljobs.filter (Job = job.isselected ());//es6$ ("#confetti-btn"). Click (Event = Playtrumpet (); Fireconfetticannon ();});
1. To write a function with multiple parameters (or no parameters or default values or deconstruction parameters), add parentheses around the parameter list.
2. The arrow function with block body does not return the value automatically. Please use the return declaration.
3. There is one thing to be aware of when creating normal objects with arrow functions. Always enclose the object in parentheses:
This point
The arrow function does not have its own this value. The value inside the this arrow function is always inherited from the enclosing range.
For methods that will be called using the Object.Method () syntax, use a non-arrow function. These functions will obtain a meaningful this value from the caller. All other content uses the arrow function.
{ ... Addall:function AddAll (Pieces) { var = this; _.each (pieces, function (piece) { self.add (piece); });} , ...} es6{... Addall:function AddAll (Pieces) { _.each (pieces, piece = This.add (piece)); }, ...}
The difference between an arrow function and a normal function
There is also a small difference between an arrow and a non-arrow function: The arrow function does not have its own arguments object.
Common functions:
1. When the function is called as a global function, this points to the global object
2. When the function is called as a method in an object, this points to the object
3. When the function is a constructor, this points to the new object of the constructor.
4, you can also change the direction of this through the Call,apply,bind
1, the arrow function does not have this, the inside of the function is from the parent's nearest non-arrow function, and cannot change the point of this.
2. Arrow function No Super
3, the arrow function is not arguments
4. The arrow function does not have a new.target binding.
5. You cannot use new
6. No prototypes
7. Duplicate named parameters are not supported.