Js--call, arrow functions/anonymous functions

Source: Internet
Author: User

The this point in JS (a few simpler):

1. This point of the normal function points to the window;

The 2 object method points to the current object;

3. () = "Arrow function points to the object when defined;

Apply and call can change the current this point, but cannot change the direction of the arrow function

functionfoo () {return() = {    return() = {      return() ={Console.log (' ID: ', This. ID);    };  }; };}varF = Foo.call ({id:1});varT1 = F.call ({id:2}) () ();//id:1vart2 = f (). Call ({id:3}) ();//id:1varT3 = f () (). Call ({id:4});//id:1

The example above shows that the this of the arrow function cannot be changed, and conversely, if it is an ordinary anonymous function, it can be changed:

function foo () {  return () = {    function() {        Console.log (  this. id);   };};} var f1 = Foo.call ({id:1}) () ()//undefined; var f2 = foo (). Call ({id:1}) ()//undefined; var f3 = foo () (). Call ({id:1})//1

The first two this finally points to window, and the last point to {id:1}

Js--call, arrow functions/anonymous functions

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.