0

For example, when we pass a callback function to Array.filter():

[1,2,3].filter(el => {
  return el === 2;
})

I'd like to inspect the internals of the method, leading up to the callback at the end

Dimoff
  • 120
  • 10
  • 1
    Then you will have to check the source code of the JavaScript engine of your browser. But the question is "why" would you want to know how the function is implemented – Andreas Jun 11 '20 at 16:25
  • The JavaScript engines are mostly open source. [V8](https://github.com/v8/v8) is the most popular, arguably. [SpiderMonkey, for Firefox](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Getting_SpiderMonkey_source_code) and [JavaScriptCore, for WebKit (Safari)](https://github.com/WebKit/webkit/tree/master/Source/JavaScriptCore) are also available. – Heretic Monkey Jun 11 '20 at 16:29

1 Answers1

2

You can't, perhaps the closest you'd get is to look at the source code for your favourite Javascript interpreter, or you could look at the polyfill code which is presented for certain methods - filter being one - on MDN

eg

if (!Array.prototype.filter){
  Array.prototype.filter = function(func, thisArg) {
    'use strict';
    if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
        throw new TypeError();

    var len = this.length >>> 0,
        res = new Array(len), // preallocate array
        t = this, c = 0, i = -1;

    var kValue;
    if (thisArg === undefined){
      while (++i !== len){
        // checks to see if the key was set
        if (i in this){
          kValue = t[i]; // in case t is changed in callback
          if (func(t[i], i, t)){
            res[c++] = kValue;
          }
        }
      }
    }
    else{
      while (++i !== len){
        // checks to see if the key was set
        if (i in this){
          kValue = t[i];
          if (func.call(thisArg, t[i], i, t)){
            res[c++] = kValue;
          }
        }
      }
    }

    res.length = c; // shrink down array to proper size
    return res;
  };
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • I think this is what he is searching for. I just add a link to the general documentation page : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference – Dony Jun 11 '20 at 16:29