“本土”是什么意思? JavaScript 中的关键字是什么意思?
我在 Chrome 的开发者控制台中偶然发现了一个名为 v8Locale
的函数。我很好奇,所以我输入了函数来获取源代码,它显示了以下代码:
function (a){
native function NativeJSLocale();
var b=NativeJSLocale(a);
this.locale=b.locale;
this.language=b.language;
this.script=b.script;
this.region=b.region;
}
我开始在互联网上搜索,发现 此文件 这似乎是来源(虽然看起来已经缩小了)。
我不知道 native
关键字在这里意味着什么。当我尝试自己做这样的事情时:
function bar() {}
function foo() {
native function bar();
}
我收到以下错误消息(实际上正如我所料):
SyntaxError: Unexpected token native
How is it possible that the v8Locale
function contains the native
token ,这是什么意思/做什么?
I stumbled upon a function called v8Locale
in Chrome's Developer Console. I was curious so I entered the function to get the source code, and it revealed the following code:
function (a){
native function NativeJSLocale();
var b=NativeJSLocale(a);
this.locale=b.locale;
this.language=b.language;
this.script=b.script;
this.region=b.region;
}
I started searching on the Internet and found this file which seems to be the source (it looks like it has been minified though).
I have no idea what the native
keyword means here. When I try to make something like this myself:
function bar() {}
function foo() {
native function bar();
}
I get the following error message (as I expected, actually):
SyntaxError: Unexpected token native
How is it possible that the v8Locale
function contains the native
token, and what does it mean/do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
用于告诉
v8
该函数是用 C++ 代码实现的That is used to tell
v8
that the function is implemented in C++ codeECMAScript 5 规范中未定义
native
关键字。听起来像是 chrome 扩展的一部分
The
native
keyword is not defined in the ECMAScript 5 specification.Sounds like it's part of a chrome extension
根据最新的 MDN Web 文档, JavaScript 中不再保留
native
一词。也可以在 Node JS 中使用:According to the latest MDN Web Docs, the
native
word is no longer reserved in javascript. Is also usable in Node JS:同样基于 ECMAScript 的 ActionScript 在这里定义了 native 关键字:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#native
他们提供了一个带有代码的示例:
并且有描述:
正如 Matt 所暗示的,标记为本机的函数是在解释器中实现的,因此您无法自己定义本机函数(除非您调整 JavaScript 解释器的源代码......)
ActionScript, which is also based on ECMAScript, defines the native keyword here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#native
They offer an example with code:
And there is the description:
As implied by Matt, functions marked as native are implemented in the interpreter so you cannot yourself define a native function (unless you tweak the source code of your JavaScript interpreter...)