$('elems').each() with fat arrow

后端 未结 2 1876
灰色年华
灰色年华 2020-12-01 21:08

I started to use ES6 fat arrow function notation and I really like it. But I am a little bit confused about it context. As far as I know, keyword this inside fat arrow funct

2条回答
  •  佛祖请我去吃肉
    2020-12-01 21:21

    Because this in the arrow function context is the same as the calling context.

    The each function provides a current element as the second argument to the callback so

    $('ul#mylist > li').each((i, el) => {
      $(el).addClass('some-class-name');
    });
    

    Or in the given case there is no need to loop

    $('ul#mylist > li').addClass('some-class-name');
    

提交回复
热议问题