你可以在 JavaScript 中找到源代码定义在哪里?

huangapple go评论81阅读模式
英文:

Where can I find source definitions in javascript?

问题

在Java和类似的编程语言中,如果我想查找某个内置函数的源代码,只需按Ctrl键并单击它,而在较新的IDE中,它会显示用同一种语言编写的源代码。然而,在JavaScript中,情况略有不同 - 源代码显示为*.d.ts。我发现有时要解决这个问题,VS Code有一个前往"Source Definition"的选项,但并不适用于所有情况。例如,如果我想查看Promise对象的正确定义(在JavaScript中),它不会显示我想要的内容。

也许在IDE中可以进行某种设置,或者我漏掉了什么?

英文:

I come from java background and similar languages where if I want to find the source of some built-in function I just press ctrl-left click on it and in newer IDEs it shows the source which is written in the same language as the language I am programming in. In javascript, however, it's a bit different - the source code is displayed as *.d.ts. I found that sometimes to solve this problem VS Code has the option to go to "Source Definition" but it doesn't work for everything. For example, if I want to see what the right definition of the Promis object is (in javascript), it just doesn't show me what I want

Maybe this can somehow be set in IDE, or am I missing something?

答案1

得分: 0

你所看到的是TypeScript类型定义文件,基本上就像Java中的接口,但只是类型。这不是源代码。

如果你想知道一个函数是如何真正实现的,你需要查看浏览器源代码(或者你计划在其中运行JavaScript的环境的源代码)。有些功能甚至不是用JavaScript实现的,而是在大多数浏览器中以C++的方式本地实现的。

由于浏览器代码库通常是难以理解的大型项目,我建议阅读相关函数的文档,比如Promise:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

英文:

What you are looking at are typescript type definition files, basically an interface like in java, but just the type. It is not source code.

If you want to know how a function is really implemented you need to look into browser source code (or the source code of whatever environment you plan to run your javascript in). Some functionally is not even implemented as javascript but natively in C++ in most browsers.

As browser codebases are massive projects that tend to be hard to understand, I recommend reading the documentation instead for the functions that interest you, for promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

huangapple
  • 本文由 发表于 2023年8月5日 05:03:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定