What does " [native code] " mean?

JavascriptJqueryNative Code

Javascript Problem Overview


I tried to investigate the jQuery code, so I used this:

document.write($.constructor);

jsfiddle

I got this result:

function Function() { [native code] }

What does [native code] mean? Why can't I see the real code?

Tested with Google-Chrome

Javascript Solutions


Solution 1 - Javascript

When you define functions in an interpreted language (as opposed to a compiled language). You have access to the file / string / text that defines the function.

In JavaScript for example you can read the definition body text of a function you have defined.

If you try to do the same for a function that is included by construction in JavaScript it is not implemented as text but as binary. There is no reason to show the binary code that implements that function because it is not readable and it might not even be available.

jQuery extends default JavaScript behaviour. It's one of the reasons it was so highly appreciated and praised as opposed to Prototype.js for example. Prototype was altering the natural behaviour of JavaScript creating possible inconsistencies when using Prototype alongside some other piece of code that relied on normal functionality.

tl;dr:

jQuery extends JavaScript, there is functionality implemented using native code (which performance wise is a good thing).

Solution 2 - Javascript

$, jQuery is just a function. Without invoking it, it's just an ordinary function. A function's constructor is Function, hence $.constructor shows [native code].

Solution 3 - Javascript

bind does this to functions:

> var f = function() { /* source code */ }; > console.log(f.toString());

function () { /* source code */ }

> var a = {}; > f = f.bind(a); > console.log(f.toString());

function () { [native code] }

> f = new Function('/* source code */'); > console.log(f.toString());

function anonymous() { /* source code */ }

> f = f.bind(a); > console.log(f.toString());

function () { [native code] }

either bind returns a reference to some kind of a wrapper code, or toString considers the bound copy native since it is not created by the user directly

However, just logging function directly, w/o using toString(), prints (in Chrome) the source code of the original unbound function:

> f = f.bind(a); > console.log(f)

ƒ () { /* source code */ }

in FF this doesn't work though - FF prints the function object, w/o the source code

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiongdoron is supporting MonicaView Question on Stackoverflow
Solution 1 - JavascriptMihai StancuView Answer on Stackoverflow
Solution 2 - JavascriptRob WView Answer on Stackoverflow
Solution 3 - JavascriptdarkdiatelView Answer on Stackoverflow