richgilbank / Scratch-JS

Run ES6, CoffeeScript and LiveScript on any page from Chrome's DevTools!
MIT License
350 stars 50 forks source link

Papa Johnny Bug #81

Closed abhisekp closed 8 years ago

abhisekp commented 8 years ago

Run the following code using any compiler in the settings

const longStr = 'Johnny Johnny, yes papa';
const name = longStr.match(/(Johnny)/g);
console.log(name[0]);
console.log(name[1]);
console.log(name);
console.log(typeof name);

Using browser JavaScript,

Output

Johnny
Johnny
["Johnny", "Johnny"]
object

Using Babel or Traceur compiler

Output :bug:

J
o
Johnny,Johnny
string
r-murphy commented 8 years ago

I took a look at this (catchy title) and it seems to be caused by DevTools' variable scoping, and not due to the transformer or the regexp. Even if you type the same directly into the console you get the bug results.

It seems to be due to the native window.name property which can only ever be a string so anything assigned to it has .toString() applied.

Using a different variable/const name other than 'name' gives the correct results, and so does wrapping it in a function.

abhisekp commented 8 years ago

@r-murphy really? :flushed: thanks. Will try that and see.

btw, wasn't the title really cachy :smirk:

richgilbank commented 8 years ago

Thanks all, this does indeed seem to be from DevTools' variable scoping. Feel free to reopen if that's not the case though!