We tried porting the following code to ES6:
function apitest(data) { data.cb(true); } function test(cb) { apitest({cb: function(data) { commit(cb,data); }}); function commit(cb,data) { cb(data); } } test(data => { document.write(data); });
This may seem a bit confusing, but it does what we expect (return true) and do not throw errors.
However, Babel translates it into:
"use strict"; function apitest(data) { data.cb(true); } function test(_cb) { apitest({ cb: function cb(data) { commit(_cb, data); } }); function commit(_cb, data) { cb(data); } } test(function (data) { document.write(data); });
This code does not work, since cb() , called inside commit() , has no underscore.
Regardless of whether you should write this code: Is our syntax faulty or is it a mistake in Babel?
I understand that the definition of cb inside an object should mask the passed parameter. Babel assigns different names to the variable used in the object and in the attached function, specifying the name of the anonymous function (why is this anyway?). After that, he should rename the function call inside commit() .
javascript ecmascript-6 babeljs
chaosflaws
source share