简体   繁体   中英

Array.prototype.flatMap() JS is behaving strangely in test cases and a repl.it snippet, but not in browser console

My apologies if this is a rookie question, but I've stumbled upon a puzzle.

Why would a JS standard function work in the chrome browser console, but not in an electron react project jest test case, or even in a repl.it snippet? Could it be a chromium thing?

The tests were passing last time I ran them a couple of weeks ago (back in 2019) The code affecting the tests hasn't been touched, (I'm fairly certain), but the project has been rebuilt recently.

This is the simple example from MDN

 let arr1 = [1, 2, 3, 4]; arr1.flatMap(x => [x * 2]); // [2, 4, 6, 8] console.log(arr1.flatMap(x => [x * 2]));

If you run the above, you will see the the SO compiler is fine with it.

BUT if I access it from a jest test case for an electron react app, it kerplunks:

在此处输入图片说明

It works fine in the browser console:

在此处输入图片说明

But it does not work in a repl .it snippet:

在此处输入图片说明

My guess would be that it is some kind of chromium thing. But I'm not sure how to confirm that.

Could somebody please direct me to the correct resource where I could learn more about this?

因为测试使用的是你电脑上安装的 node.js 版本,升级到第 12 个版本,你的版本中还没有提供 flatMap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM