= = applied in JS callback function

Source: Internet
Author: User

= = can simplify the invocation of the previous callback function, specifically:

In the future, almost all callback functions can be simplified by using the arrow function

Like what:

1. All callback functions can: go to function change =

2. If the function body has only one sentence: can omit {}

If this sentence is still return, you can omit return

3. If there is only one parameter: can omit ()

However, if there are no parameters, you must leave the empty ()

Larger use: Arrow functions are shared with the same this--in and out of bind

Special: If you do not want to share this inside and outside, you cannot use the arrow function

such as the event handler function:

Elem.addeventlistener ("click", Function () {This->elem})

Elem.addeventlistener ("click", () =>{this-> not Elem})

Workaround:

Elem.addeventlistener ("Click", e=>{E.target->elem})

vararr=[12,2,123,23,1,3]; //Arr.sort (function (A, b) {return -a-B;})Arr.sort (A, b) =>a-b);  Console.log (arr); varNewarr=arr.map (val=>val*2);  Console.log (String (NEWARR)); varbob={sname:"Bob", friends:["Jack", "Rose", "Tom", "Jerry"], Intr () {//This->bob       This. Friends.foreach (Val=Console.log ( This. sname+ "Know" +val));  }} bob.intr (); varBtns=document.getelementsbytagname ("button"); //This->window   for(varI= 0,len=btns.length;i<len;i++) {Btns[i].addeventlistener ("Click",      //() =>alert (this.innerhtml)//ErrorE=>alert (e.target.innerhtml))}

= = applied in JS callback function

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.