javascript - 为什么新的 ES6 find 方法在 JSBin 中无法识别?

标签 javascript ecmascript-6 shim jsbin

我认为以下代码是正确的,并且启用了转译器 ES6/Babel 选项,但出现以下错误。

"error"
"TypeError: nums.find is not a function
    at qezoze.js:11:18
    at qezoze.js:16:3
    at https://static.jsbin.com/js/prod/runner-3.34.2.min.js:1:13603
    at https://static.jsbin.com/js/prod/runner-3.34.2.min.js:1:10524"

这是我在 JS bin ( https://jsbin.com/qezoze/edit?js,console ) 中的代码

/*jshint esnext: true */ 

(function() {

  'use strict';

  let nums = [1,2,3,4,5];

  let one = nums.find(num => num === 1);


  console.log(one);

}());

最佳答案

JSBin 正在转译代码,但没有加载完全支持所需的垫片。如果我们检查正在运行的文档,我们可以看到这一点:

enter image description here

请注意,除了您的代码之外,没有 script 元素。虽然可以从窗口外添加到 Array.prototype,但假设没有设置任何东西来延迟您的代码以让 JSBin 有机会这样做(并且考虑到 Array.prototype.find 显然实际上丢失了),显然它们只是不包括垫片。

It works在 Babel 的 REPL 上。

关于javascript - 为什么新的 ES6 find 方法在 JSBin 中无法识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113604/

相关文章:

javascript - 如何创建一个表,用户可以在其中更改影响 AngularJS 结果的值

javascript - 如何在 ES6 中将渲染函数中的 Jsx 分离到一个单独的文件中?或任何其他解决方案以将逻辑和表示分开?

javascript - 似乎无法执行传递给变量的函数

javascript - 在 Node 子进程中运行 ES6 导入语法

html - 在 grails 中使用 webshim

javascript - 在连接的字符串之间动态添加空格

javascript - 如何防止浏览器拦截和解释按键

javascript - react native : how to use require(path) with dynamic urls?

PHP 5.3 - 5.4 垫片

javascript - HTML5 shim 回退?