为什么Javascript内置方法/函数是用C / C ++而不是JS语法编写的 [英] Why Javascript in-built methods/functions are written in C/C++ and not JS syntax

查看:191
本文介绍了为什么Javascript内置方法/函数是用C / C ++而不是JS语法编写的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是参考这个老问题其中 - can-i-find-javascript-native-functions-source-code

This question is in reference to this old question Where-can-i-find-javascript-native-functions-source-code

该页面的答案是,源代码在 c c ++ 但我很好奇为什么源(定义)是用这些语言的?我的意思是它们是JS函数定义,例如 toString()方法。它是一个JavaScript函数,因此它的定义必须使用Javascript语法编写。

The answer on that page says, that the source code is in c or c++ but I am curious as to why the source (definition) is in those languages? I mean they are JS functions definitions for e.g toString() method. It's a JavaScript function so its definition must be written using Javascript syntax.

toString; 在chrome控制台输出中 function toString(){[native code]}

toString; in chrome console outputs function toString() { [native code] }.

如果它是用户定义的函数,那么你可以看到定义但不是 toString()或者就此而言其他内置函数
毕竟它们只是函数/方法,必须在JavaScript语法中定义,以便引擎正确解释它们。

If it's a user-defined function then you can see the definition but not toString() or for that matter other in-built functions after all they are just function/methods that must be defined in JavaScript syntax for the engine to interpret them correctly.

我希望你可以理解我想要做什么。

I hope you can understand what point I am trying to make.

推荐答案

正如评论中指出的那样,你对JavaScript如何有一个根本的误解在这个术语最纯粹的意义上,JavaScript是一种脚本语言,即它是用于编写主机环境脚本的工作。

As pointed out in the comments, you have a fundamental misunderstanding of how JavaScript works.

。它意味着嵌入在一个更大的系统中(在这种情况下,用C / C ++编写的Web浏览器)以有限的方式操作该系统。

JavaScript is a scripting language, in the purest sense of that term, i.e. it is meant to script a host environment. It is meant to be embedded in a larger system (in this case, a web browser written in C/C++) to manipulate that system in a limited way.

其他一些例如,bash是unix的脚本语言,python是sublime文本编辑器的脚本语言,elisp是emacs的脚本语言,lua是魔兽世界的脚本语言等。

Some other examples would be bash as the scripting language of unix, python as the scripting language of the sublime text editor, elisp as the scripting language of emacs, lua as the scripting language for World of Warcraft, etc.

当我们说某个函数是内置时,我们的意思是它实际上是托管环境的函数(例如Web浏览器),而不是脚本语言(JavaScript)的函数。

When we say a function is 'built-in', we mean it is actually a function of the hosting environment (e.g. web-browser), not a function of the scripting language (JavaScript).

尽管JavaScript标准规定了某些内置函数,但这意味着符合标准的主机环境需要公开该功能,而不管底层实现的语言是什么。

Although the JavaScript standard mandates certain built in functions, all that means is that a conforming host environment needs to expose that functionality, regardless of what language the underlying implementation is in.

这篇关于为什么Javascript内置方法/函数是用C / C ++而不是JS语法编写的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆