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
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
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;
};
}