Home  >  Q&A  >  body text

javascript - es6中this

Beginner es6
A previous click event was rewritten as $(".btn").click(()=>{console.log(this)});This here points to the document, but if When using function, this points to the current element. How to change the point of this?

高洛峰高洛峰2663 days ago966

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-07-05 11:09:39

    It is recommended to take a look at the arrow function of es6

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-05 11:09:39

    The arrow function does not have its own this at all, but refers to the outer this

    reply
    0
  • 阿神

    阿神2017-07-05 11:09:39

    This question is a cliché. In conclusion, the this pointer in the arrow function has been fixed and points to the block-level scope of the current code rather than the caller's scope.

    Some related documents:

    https://developer.mozilla.org...

    http://www.infoq.com/cn/artic...

    reply
    0
  • Cancelreply