英文:
Why does VS Code colour JavaScript's `Math` and `Number` differently?
问题
Take Math.random and Number.MAX_VALUE function.
为什么Number是绿色,而其他是蓝色(适用于我的颜色主题)。这也适用于Number上的静态方法。
英文:
Take Math.random and Number.MAX_VALUE function.
Why is Number green and the other Blue (for my colour theme). This also applies to static methods on Number.
答案1
得分: 3
-
对于
Math
:"语义令牌类型:变量" -
对于
Number
:"语义令牌类型:类"
这我认为是有道理的。在 lib.es5.d.ts 中:
interface NumberConstructor {
new(value?: any): Number;
...
}
...
declare var Number: NumberConstructor;
...
interface Math {
... // 没有构造函数 (`new` 函数)
}
declare var Math: Math;
NumberConstructor
类型声明中的 new
函数为 Number
构造函数提供了类型信息,这表明对 TypeScript 来说 Number
是一个类。但对于 Math
类型则不是这样。
英文:
If you inspect the token scopes using Developer: Inspect Editor Tokens and Scopes
, you'll see:
-
For
Math
: "semantic token type: variable" -
For
Number
: "semantic token type: class"
This makes sense I think. In lib.es5.d.ts:
interface NumberConstructor {
new(value?: any): Number;
...
}
...
declare var Number: NumberConstructor;
...
interface Math {
... // no constructor (`new` function)
}
declare var Math: Math;
The new
function in the NumberConstructor
type declaration is providing typings for the Number
constructor, which indicates to TypeScript that Number
is a class. But not so for the Math
type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论