为什么 JavaScript 中的对象没有 find 方法

why object in javascript does not have find method?

本文关键字:find 方法 对象 JavaScript 为什么      更新时间:2023-09-26

我开始node repl 并输入了这个

> function isPrime(element, index, array) {
...   var start = 2;
...   while (start <= Math.sqrt(element)) {
.....     if (element % start++ < 1) {
.......       return false;
.......     }
.....   }
...   return element > 1;
... }
undefined
> [4, 6, 8, 12].find(isPrime)
TypeError: Object 4,6,8,12 has no method 'find'
    at repl:1:16
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)

这是怎么回事?为什么数组对象没有 find 方法?不是和这里一样吗?

V8暂时没有意识到.find()数组方法(请参阅源代码)。所以node.jsio.js不支持它。请参阅兼容性表