Jose Hermosilla Rodrigo
Jose Hermosilla Rodrigo

Reputation: 3683

Different variable scoping using "let" keyword between browsers

The below piece of code, returns different behaviuours if you execute it in Chrome and Firefox.

var obj = {
  key: 'val',
  key2: 'val'
};

function x(b) {
  setTimeout(b, 1000);
}

var keys = Object.keys(obj);
for (let ki of keys) {
  x(function() {
    console.log(ki); // Prints key2 key2 in Firefox, key key2 in Chrome
  });
}

Why is happening this ? It's a bug ? Should be the right output the one returned by Chrome taking in count the specs ?

Upvotes: 1

Views: 36

Answers (1)

Christopher Davies
Christopher Davies

Reputation: 4551

I'd say it's a bug. You can try running it on jsBin with Babel enabled, and you see the Chrome behavior in FireFox:

http://jsbin.com/nadikatari/edit?html,js,console,output

Sounds like FireFox hasn't implemented let yet. You really should be transpiling to ES5 using Babel, if you want to use ES6 at this point.

Upvotes: 1

Related Questions