TypeScript 中的枚举:JavaScript 代码在做什么? [英] Enums in TypeScript: what is the JavaScript code doing?
问题描述
以下打字稿:
enum PrimaryColors { Red, Green, Blue };
生成以下 JavaScript:
var PrimaryColors;(功能(原色){PrimaryColors[PrimaryColors["Red"] = 0] = "Red";PrimaryColors[PrimaryColors["Green"] = 1] = "Green";PrimaryColors[PrimaryColors["Blue"] = 2] = "Blue";})(PrimaryColors || (PrimaryColors = {}));;
我很尴尬地承认我不明白 JavaScript 在做什么.
括号中的函数是使用另一个分配作为索引/键来分配字符串值.我以前从未见过这样的事情.
函数后面的 (PrimaryColors || (PrimaryColors = {}) 的目的是什么?
如果答案是正确地学习 JavaScript,我会欣然接受它,只要它附带一个建议的来源,清楚地解释我在这里看到的内容.
我相信:
PrimaryColors[PrimaryColors["Red"] = 0] = "Red";
相当于:
PrimaryColors[0] = "红色";原色["红色"] = 0;
请参阅此参考.><块引用>
表达式 x = 7 是第一种类型的示例.这个表情使用 = 运算符将值 7 赋给变量 x.这表达式本身的计算结果为 7.
例如:
console.log((x = 7));
输出:
7
同样:
var x = {};console.log((x["hi"] = 7));
也输出 7.
至于第二件事,PrimaryColors
最初是未定义的.
var x;控制台日志(x);//不明确的
在布尔上下文中,undefined
的计算结果为 false
:
console.log(!undefined);//真的控制台日志(!!未定义);//错误的
健全性检查:
console.log((!undefined) === true);//真的console.log((!!undefined) === false);//真的console.log(undefined === false);//错误的
这是短路的常见用法.因为 PrimaryColors
最初是未定义的 (false),它会将 {}
传递给函数.
PrimaryColors ||(原色 = {})
The following TypeScript:
enum PrimaryColors { Red, Green, Blue };
Produces the following JavaScript:
var PrimaryColors;
(function (PrimaryColors) {
PrimaryColors[PrimaryColors["Red"] = 0] = "Red";
PrimaryColors[PrimaryColors["Green"] = 1] = "Green";
PrimaryColors[PrimaryColors["Blue"] = 2] = "Blue";
})(PrimaryColors || (PrimaryColors = {}));
;
I am embarrassed to admit that I don't understand what the JavaScript is doing.
The function in parentheses is assigning string values using another assignment as the index/key. I have not seen anything like this before.
And what is the purpose of the (PrimaryColors || (PrimaryColors = {}) following the function?
If the answer is to learn JavaScript properly, I will readily accept it, provided it comes with a suggested source that clearly explains what I am seeing here.
I believe:
PrimaryColors[PrimaryColors["Red"] = 0] = "Red";
is equivalent to:
PrimaryColors[0] = "Red";
PrimaryColors["Red"] = 0;
See this reference.
The expression x = 7 is an example of the first type. This expression uses the = operator to assign the value seven to the variable x. The expression itself evaluates to seven.
For example:
console.log((x = 7));
outputs:
7
Similarly:
var x = {};
console.log((x["hi"] = 7));
Also outputs 7.
As for the second thing, PrimaryColors
is initially undefined.
var x;
console.log(x); // undefined
In a boolean context, undefined
evaluates to false
:
console.log(!undefined); // true
console.log(!!undefined); // false
Sanity check:
console.log((!undefined) === true); // true
console.log((!!undefined) === false); // true
console.log(undefined === false); // false
This is a common usage of short circuiting. Because PrimaryColors
is initially undefined (false), it will pass {}
to the function.
PrimaryColors || (PrimaryColors = {})
这篇关于TypeScript 中的枚举:JavaScript 代码在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!