简体   繁体   English

如何获取原生 javascript 函数源代码?

[英]How to get native javascript functions source code?

In console it appears as native function but I'd like to know how they were constructed.在控制台中,它显示为本机功能,但我想知道它们是如何构造的。 For example what is the code which executes when pressing space bar to scroll the page.例如,按下空格键滚动页面时执行的代码是什么。 That info would teach me a lot, and I could make my functions more effective这些信息会教给我很多东西,我可以让我的功能更有效

  1. Pick a browser选择浏览器
  2. Make sure it is an open source one确保它是开源的
  3. Dig through its source code挖掘它的源代码

Some repositories include:一些存储库包括:

Note that JavaScript native functions are generally not written in JavaScript (expect C or C++ most of the time).请注意,JavaScript 原生函数通常不是用 JavaScript 编写的(大多数情况下是 C 或 C++)。 They are just exposed to JS through an API.它们只是通过 API 暴露给 JS。

Also note that the code that scrolls a page when the spacebar is pressed isn't even a function that is exposed to JS.另请注意,按下空格键时滚动页面的代码甚至不是向 JS 公开的函数。

While this will not show you actual source code, if you're interested in how many of the native JavaScript functions are implemented, you can peruse the specification upon which they are based:虽然这不会向您显示实际的源代码,但如果您对实现了多少本机JavaScript函数感兴趣,您可以仔细阅读它们所基于的规范:

Standard ECMA-262 标准 ECMA-262

What is V8?什么是V8?

  • V8 is Google's open source JavaScript engine. V8 是 Google 的开源 JavaScript 引擎。
  • V8 implements ECMAScript as specified in ECMA-262. V8 实现了 ECMA-262 中指定的 ECMAScript。
  • V8 is written in C++ and is used in Google Chrome, the open source browser from Google. V8 是用 C++ 编写的,用于谷歌的开源浏览器谷歌 Chrome。
  • V8 can run standalone, or can be embedded into any C++ application. V8 可以独立运行,也可以嵌入到任何 C++ 应用程序中。
  • V8 project page: https://github.com/v8/v8/wiki V8 项目页面: https ://github.com/v8/v8/wiki
  • V8 source code: https://github.com/v8/v8 V8源代码: https ://github.com/v8/v8

The contract for exactly how the Javascript built-ins should behave is outlined in the ECMAScript specification (see example for Array.every() ). ECMAScript 规范中概述了 Javascript 内置函数的确切行为方式(参见Array.every()的示例)。

There are a number of different Javascript engines, each with their own specific implementation of ECMAScript.有许多不同的 Javascript 引擎,每个引擎都有自己特定的 ECMAScript 实现。 The most common Javascript engines are (links point to code for Array.every() ):最常见的 Javascript 引擎是(链接指向Array.every()的代码):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM