8DK
8DK

Reputation: 714

Array.prototype.concat() under the hood

How do I see what code is inside the function concat? How does it do what it does? Does anyone have a copy of the code or a way to see it in the browsers console?

console.dir does not give me access past

console.dir(Array.prototype.concat);
function concat() { [native code] }
arguments: null
caller: null
length: 1
name: "concat"
__proto__: function Empty() {}
<function scope>

I can't or don't know how to inspect this but there must be a way to dig into javascript functions

Upvotes: 2

Views: 1314

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328614

Array comes with JavaScript, so it depends on your JavaScript engine how it is implemented. A JS engine is free to implement it in any way. Chances are that it doesn't use JavaScript because that might be too slow or might not be possible because you'd need a JavaScript engine with the feature you're trying to implement to implement it (see bootstrapping).

In most Browsers, many JavaScript functions are implemented in C/C++. Here is an example from the source of the Chrome/Chromium family of browsers: https://cs.chromium.org/chromium/src/v8/src/builtins/builtins-array.cc?q=Array.prototype.concat&sq=package:chromium&dr=C&l=635

ArrayConcatJS becomes Array.prototype.concat in the InstallFunctions call in Chrome bootstrapper. Kudos for this go to apsillers.

Array.concat for the Rhino engine can be found here: https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/NativeArray.java in the method js_concat() (like 1322).

Upvotes: 5

Related Questions