“本土”是什么意思? JavaScript 中的关键字是什么意思?

发布于 2024-11-24 06:00:57 字数 850 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我ぃ本無心為│何有愛 2024-12-01 06:00:57

用于告诉 v8 该函数是用 C++ 代码实现的

That is used to tell v8 that the function is implemented in C++ code

抱着落日 2024-12-01 06:00:57

ECMAScript 5 规范中未定义 native 关键字。

听起来像是 chrome 扩展的一部分

The native keyword is not defined in the ECMAScript 5 specification.

Sounds like it's part of a chrome extension

泪意 2024-12-01 06:00:57

根据最新的 MDN Web 文档, JavaScript 中不再保留 native 一词。也可以在 Node JS 中使用:

alec@MacBook-Air ~/process/tmp/shed/cli (main) $ node -v
v16.9.1
alec@MacBook-Air ~/process/tmp/shed/cli (main) $ node
...
> let native = 'XLM'
undefined
> console.log(native)
XLM
undefined
> 

According to the latest MDN Web Docs, the native word is no longer reserved in javascript. Is also usable in Node JS:

alec@MacBook-Air ~/process/tmp/shed/cli (main) $ node -v
v16.9.1
alec@MacBook-Air ~/process/tmp/shed/cli (main) $ node
...
> let native = 'XLM'
undefined
> console.log(native)
XLM
undefined
> 
山川志 2024-12-01 06:00:57

同样基于 ECMAScript 的 ActionScript 在这里定义了 native 关键字:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#native

他们提供了一个带有代码的示例:

native function functionName();
class className { 
  native function methodName();
}

并且有描述:

指定函数或方法由 Flash Player 以本机代码实现。 Flash Player 在内部使用 native 关键字来声明 ActionScript 应用程序编程接口 (API) 中的函数和方法。该关键字不能在您自己的代码中使用。

正如 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:

native function functionName();
class className { 
  native function methodName();
}

And there is the description:

Specifies that a function or method is implemented by Flash Player in native code. Flash Player uses the native keyword internally to declare functions and methods in the ActionScript application programming interface (API). This keyword cannot be used in your own code.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文