The format of the arrow function in JS and the difference between &this and ordinary function

Source: Internet
Author: User
Tags addall
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.