Javascript “[本机代码]”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11234664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:02:34  来源:igfitidea点击:

What does " [native code] " mean?

javascriptjquerynative-code

提问by gdoron is supporting Monica

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

我试图调查 jQuery 代码,所以我使用了这个:

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?

什么[native code]意思?为什么我看不到真正的代码?

Tested with Google-Chrome

测试过 Google-Chrome

采纳答案by Mihai Stancu

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.

例如,在 JavaScript 中,您可以读取已定义函数的定义正文文本。

If you try to do the same for a function that is includedby 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.

如果您尝试对JavaScript 中通过构造包含的函数执行相同操作,则它不会以文本形式实现,而是以二进制形式实现。没有理由显示实现该功能的二进制代码,因为它不可读,甚至可能不可用。

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 alteringthe natural behaviour of JavaScript creating possible inconsistencies when using Prototype alongside some other piece of code that relied on normal functionality.

jQuery 扩展了默认的 JavaScript 行为。例如,这就是它与 Prototype.js 相比受到高度赞赏和赞扬的原因之一。Prototype 正在改变JavaScript 的自然行为,当将 Prototype 与其他一些依赖于正常功能的代码一起使用时,可能会产生不一致的情况。

tl;dr:

tl;博士:

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

jQuery 扩展了 JavaScript,使用本机代码实现了一些功能(这在性能方面是一件好事)。

回答by Rob W

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

$,jQuery只是一个函数。不调用它,它只是一个普通的函数。函数的构造函数是Function,因此$.constructor显示[native code]