0

I would like to know how array methods are built-in (the source code), for example this prototype todos() is how i imagine the every() method works behind the scenes, i would like to find that documentation.

Array.prototype.todos = function(fn) {
    for(let item of this) {
        if(!fn(item)) {
            return false
        }
    }
    return true
}

const result = [1, 2, 3].todos(x => x < 10)
console.log(result)
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35
  • There is no universal implementation. Chromium + v8 has its own implementation in c++, Bun has its own implementation through JavaScriptCore, etc – Nino Filiu Jul 17 '22 at 00:58
  • Which javascript engine? V8? JavascriptCore? SpiderMonkey? You may imagine that "every" works like that, but the implementation is probably not written in javascript – Jaromanda X Jul 17 '22 at 00:59
  • 1
    Do you mean something like this? https://stackoverflow.com/questions/22371251/how-to-get-native-javascript-functions-source-code – shtef21 Jul 17 '22 at 00:59
  • All the javascript engines are open source (yes, even Microsoft's old Chakra engine). You are free to download and read them. This is actually a good exercise on how to read complex codebases. Download all of them (or just browse them in github) and try to find the implementation of the array methods – slebetman Jul 17 '22 at 02:44

1 Answers1

2

The contract for exactly how the Javascript built-ins should behave is outlined in the ECMAScript specification.

There are a number of different Javascript engines, each with their own specific implementation of ECMAScript. The most common Javascript engines are:

Codebling
  • 10,764
  • 2
  • 38
  • 66
  • 1
    gecko and webkit are not javascript engines - did you mean SpiderMonkey and JavaScriptCore? or did you mean Blink instead of V8? – Jaromanda X Jul 17 '22 at 01:32