5

I had been playing around with JavaScript examples (in Google Chrome's Developer Console), and noticed this representation [native code].

Example Image for native code

To my understanding: it seems to be something that comes as part of JavaScript itself

but that doesn't kill my curiosity. I'm looking for a better explanation to understand this [native code]. So, precisely, I have these doubts:

  1. What qualifies as [native code]? Is it supposed to be just the functions only (function declarations), or can be any block of JavaScript code?
  2. Can we turn our custom written code into a [native code]? If yes, how?

PS: I know SO guidelines suggest we shouldn't be asking multiple questions in one post. However, above two are related and clarifies our understanding for a single topic [native code], hence asked both together here.


Here's the working example, that prints [native code]:

var doWork = function (name="Aaditya") {
 console.log(name);
};
doWork(); // prints -- Aaditya
console.log(doWork); // prints -- ƒ (name="Aaditya") { return name; }
console.log(doWork.toString); // prints -- ƒ toString() { [native code] }

Edit:

I see that there is another similar post on SO What does “[native code]” mean?, but apparently, I haven't got an answer there (so, not sure if this should really be marked as a duplicate of that post or not, please suggest/help).

Aaditya Sharma
  • 3,330
  • 3
  • 24
  • 36
  • 2
    It's code that's made *visible* to JavaScript via some browser APIs, but it's internally implemented as non-JavaScript (probably C/C++) code. – Pointy Jul 30 '19 at 18:10
  • I believe native code refers to those APIs that are baked into the JavaScript language or into the DOM API. For instance [`Function.prototype.toString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString) is part of JavaScript proper-- it is a method that is "under the hood", _not_ something in the code loaded from a ` – Alexander Nied Jul 30 '19 at 18:12
  • 4
    It's code that is native, i.e. run directly on the processor. It's implemented by the browser itself. It's not interpreted JS code, and you cannot turn your JS code into native code. Such functions can be found in the browser's source code. –  Jul 30 '19 at 18:14
  • Regarding the edit by the OP, try reading the other answers on the dupe, not just the accepted answer. –  Jul 30 '19 at 18:37
  • @Amy your comment precisely answers my question. Thanks! – Aaditya Sharma Jul 30 '19 at 18:37

0 Answers0