Using jQuery $(this) with ES6 Arrow Functions (lexical this binding)

前端 未结 5 623
一个人的身影
一个人的身影 2020-11-22 00:02

Using ES6 arrow functions with lexical this binding is great.

However, I ran into an issue a moment ago using it with a typical jQuery click binding:

5条回答
  •  -上瘾入骨i
    2020-11-22 00:21

    Event binding

    $button.on('click', (e) => {
        var $this = $(e.currentTarget);
        // ... deal with $this
    });
    

    Loop

    Array.prototype.forEach.call($items, (el, index, obj) => {
        var $this = $(el);
        // ... deal with $this
    });
    

提交回复
热议问题