Javascript 正在将此编译为未定义?

Javascript 正在将此编译为未定义?,javascript,node.js,ecmascript-6,babeljs,Javascript,Node.js,Ecmascript 6,Babeljs,我的app.js是用es6标准编写的,我的问题是> 我如何使用这样的代码 $('img').each(() => { var title = $(this).attr('src'); titles.push(title); }); 我观察到node将此代码编译为: 'use strict'; $('img').each(function () { var title = $(undefined).attr('src'); titles.push(titl

我的app.js是用es6标准编写的,我的问题是> 我如何使用这样的代码

$('img').each(() => {
    var title = $(this).attr('src');
    titles.push(title);
});
我观察到node将此代码编译为:

'use strict';

$('img').each(function () {
    var title = $(undefined).attr('src');
    titles.push(title);
});
将“this”编译为“undefined”,如何修复?
提前支付thx;)

箭头函数按词汇绑定
值-请参阅

因为这已经在词汇上进行了绑定,所以调用arrow函数 通过call()或apply()方法只能传入参数,但 对这一点没有影响:


只需使用一个普通的旧函数即可解决此问题:)

另一个解决方案是使用传递给回调的元素,如文档-函数(整数索引,元素)中所定义:

var titles=[];
$('img')。每个((索引,元素)=>{
var title=$(元素).attr('src');
标题。推送(标题);
});
document.getElementById('demo').innerHTML=JSON.stringify(标题,未定义,4)

请阅读