1

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

apsillers
  • 112,806
  • 17
  • 235
  • 239
8DK
  • 704
  • 1
  • 5
  • 15
  • There is indeed: have a browse around the C++ code for whatever engine you're using. (If your JS engine does not publish its source code, you're out of luck.) – apsillers Oct 10 '14 at 12:47
  • 1
    possible duplicate of [Read JavaScript native code](http://stackoverflow.com/questions/9103336/read-javascript-native-code) – apsillers Oct 10 '14 at 12:49
  • 2
    To narrowly attempt an answer at your specific question, here's Chromium's implementation of `concat`: https://code.google.com/p/chromium/codesearch#chromium/src/v8/src/array.js&sq=package:chromium&l=473 (`ArrayConcatJS` becomes `Array.prototype.concat` in the `InstallFunctions` call on line 1519) – apsillers Oct 10 '14 at 13:01

1 Answers1

4

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).

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820