为什么VS Code会以不同颜色显示JavaScript中的 `Math` 和 `Number`?

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

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.

为什么VS Code会以不同颜色显示JavaScript中的 `Math` 和 `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.

huangapple
  • 本文由 发表于 2023年4月7日 05:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953759.html
匿名

发表评论

匿名网友

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

确定