简体   繁体   English

如何找到原生 JavaScript 函数的实现代码

[英]How to find the implementation code of a vanilla JavaScript function

A colleague and I were discussing the implementation of the JavaScript "if" statement and I wanted to find the implementation so we could read through it and hopefully get a bit wiser.我和一位同事正在讨论 JavaScript“if”语句的实现,我想找到实现,以便我们可以通读它并希望能更明智一些。

However, I wasn´t able to find it anywhere, so my question is;但是,我无法在任何地方找到它,所以我的问题是; Where do you go to read the JavaScript source code?你去哪里阅读 JavaScript 源代码?

if is not a function but is a keyword. if不是函数,而是关键字。 There is no JavaScript implementation of it.它没有 JavaScript 实现。

The compiler/transpiler understands the if statement just like it understands other tokens from the var , const , and let to do {} while();编译器/转译器理解if语句,就像它理解来自varconstlet to do {} while();的其他标记一样。 and for .并且for . All of these are keywords with special meaning known to the JavaScript interpreter.所有这些都是 JavaScript 解释器已知的具有特殊含义的关键字。

You would want to look the implementation of the interpreter/compiler and runtime implementation to understand how it handles the various control keywords such as if.您可能希望查看解释器/编译器和运行时实现的实现,以了解它如何处理各种控制关键字,例如 if。 Alternatively you can look at the specs of the language.或者,您可以查看语言的规范。

Edit:编辑:

It is worth noting that JavaScript is ECMAScript.值得注意的是,JavaScript 是 ECMAScript。 There are ECMAScript standards and JavaScript implements these.有 ECMAScript 标准,JavaScript 实现了这些标准。 So concepts such as truthiness and falsiness (whether a value is truthy of falsey) is in the ECMAScript standards.因此,诸如真假(一个值是真还是假)之类的概念在 ECMAScript 标准中。

Particularly, you can check section 13.6.7 that speaks to the semantics of the if statement: https://262.ecma-international.org/11.0/#sec-if-statement-runtime-semantics-evaluation特别是,您可以查看第 13.6.7 节,其中涉及if语句的语义: https ://262.ecma-international.org/11.0/#sec-if-statement-runtime-semantics-evaluation

From there, one can find what it means for something to be truthy/falsey, which is ToBoolean , as defined in the specs (not a JavaScript function):从那里,人们可以找到真/假的含义,即ToBoolean ,如规范中所定义(不是 JavaScript 函数):

在此处输入图像描述

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

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