jlongster / es6-macros

A collection of sweet.js macros that implement ES6 features for ES5
BSD 2-Clause "Simplified" License
237 stars 18 forks source link

Getting "arguments is not defined" error using fat arrow in the browser? #14

Closed nicolashery closed 10 years ago

nicolashery commented 10 years ago

I'm new to ES6 and Sweet.js, so apologies if I'm missing something obvious :)

Given this source:

// src/index.js
var addOne = v => v + 1;
var evens = [2, 4, 6];
var odds = evens.map(addOne);
console.log(odds);

Running sjs src/index.js -m es6-macros -o bundle.js -c produces:

// bundle.js
var __ref$1194 = function (__fa_args$1205, v$1206) {
        return v$1206 + 1;
    }.bind(this, arguments);
var addOne$1196 = __ref$1194;
var __ref$1198 = [
        2,
        4,
        6
    ];
var evens$1200 = __ref$1198;
var __ref$1202 = evens$1200.map(addOne$1196);
var odds$1204 = __ref$1202;
console.log(odds$1204);

And I get Uncaught ReferenceError: arguments is not defined when running bundle.js in the browser. However, it seems to work fine using node bundle.js.

What am I missing? Thanks!

nicolashery commented 10 years ago

Hum, wrapping it in an IEF seems to do the trick for the browser:

// src/index.js
(function() {
  var addOne = v => v + 1;
  var evens = [2, 4, 6];
  var odds = evens.map(addOne);
  console.log(odds);
}());

Maybe that's just it? I guess since I'm using webpack or browserify usually, they do it for me so that's fine?

jlongster commented 10 years ago

You found a bug! Sorry, I added that recently without thinking it through. Will fix right now and publish a new version.

jlongster commented 10 years ago

Yeah, it binds arguments from the outer scope (ES6 semantics), but I forgot to check if arguments even exists. This is still early!

nicolashery commented 10 years ago

Glad I could help! Learned a thing about ES6 along the way too :)

jlongster commented 10 years ago

Fixed and published in npm, so just do npm install es6-macros again.

nicolashery commented 10 years ago

Wonderful! Installed 0.0.6 and ran my example again, works great!

Thanks for quickly fixing this!

thieuanh1995hn commented 6 years ago

It's still appear in my chrome browser . Why ?