ts-learnings/_book/07_enums.html
2019-04-04 16:11:16 +08:00

540 lines
39 KiB
HTML

<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>枚举 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="08_type_inference.html" />
<link rel="prev" href="06_generics.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="01_basic_data_types.html">
<a href="01_basic_data_types.html">
基本数据类型
</a>
</li>
<li class="chapter " data-level="1.3" data-path="02_variables_declaration.html">
<a href="02_variables_declaration.html">
变量声明
</a>
</li>
<li class="chapter " data-level="1.4" data-path="03_classes.html">
<a href="03_classes.html">
</a>
</li>
<li class="chapter " data-level="1.5" data-path="04_interfaces.html">
<a href="04_interfaces.html">
接口
</a>
</li>
<li class="chapter " data-level="1.6" data-path="05_functions.html">
<a href="05_functions.html">
函数
</a>
</li>
<li class="chapter " data-level="1.7" data-path="06_generics.html">
<a href="06_generics.html">
泛型
</a>
</li>
<li class="chapter active" data-level="1.8" data-path="07_enums.html">
<a href="07_enums.html">
枚举
</a>
</li>
<li class="chapter " data-level="1.9" data-path="08_type_inference.html">
<a href="08_type_inference.html">
类型推导
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >枚举</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="&#x679A;&#x4E3E;&#xFF08;enums&#xFF09;">&#x679A;&#x4E3E;&#xFF08;Enums&#xFF09;</h1>
<p>Enum [en^m]&#x662F;&#x6E90;&#x81EA;Enumerate, &#x610F;&#x601D;&#x662F;&#x4E00;&#x4E00;&#x5217;&#x4E3E;&#x51FA;&#x6765;&#x3002;</p>
<p>&#x679A;&#x4E3E;&#x7279;&#x6027;&#x4EE4;&#x5230;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x547D;&#x540D;&#x5E38;&#x91CF;&#x7684;&#x96C6;&#x5408;&#x53EF;&#x884C;&#x3002;&#x4F7F;&#x7528;&#x679A;&#x4E3E;&#x53EF;&#x4F7F;&#x5F97;&#x610F;&#x56FE;&#x8868;&#x8FBE;&#xFF0C;&#x6216;&#x521B;&#x5EFA;&#x5DEE;&#x5F02;&#x6848;&#x4F8B;&#x66F4;&#x4E3A;&#x5BB9;&#x6613;&#xFF08;Using enums can make it easier to document intent, or create a set of distinct cases&#xFF09;&#x3002;TypeScript&#x540C;&#x65F6;&#x652F;&#x6301;&#x57FA;&#x4E8E;&#x6570;&#x5B57;&#x4E0E;&#x5B57;&#x7B26;&#x4E32;&#x8FD9;&#x4E24;&#x79CD;&#x679A;&#x4E3E;&#x3002;</p>
<h2 id="&#x6570;&#x5B57;&#x7684;&#x679A;&#x4E3E;&#xFF08;numeric-enums&#xFF09;">&#x6570;&#x5B57;&#x7684;&#x679A;&#x4E3E;&#xFF08;Numeric enums&#xFF09;</h2>
<p>&#x8FD9;&#x91CC;&#x5C06;&#x9996;&#x5148;&#x4EE5;&#x6570;&#x5B57;&#x679A;&#x4E3E;&#x5F00;&#x59CB;&#xFF0C;&#x5982;&#x6709;&#x7740;&#x5176;&#x5B83;&#x8BED;&#x8A00;&#x7684;&#x7ECF;&#x9A8C;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x79CD;&#x679A;&#x4E3E;&#x53EF;&#x80FD;&#x66F4;&#x4E3A;&#x719F;&#x6089;&#x3002;&#x4F7F;&#x7528;<code>enum</code>&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x51FA;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> Direction {
Up = <span class="hljs-number">1</span>,
Down,
Left,
Right,
}
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x7684;&#x679A;&#x4E3E;&#xFF0C;&#x5176;&#x4E2D;<code>Up</code>&#x4EE5;<code>1</code>&#x8FDB;&#x884C;&#x4E86;&#x521D;&#x59CB;&#x5316;&#x3002;&#x5176;&#x540E;&#x7684;&#x6240;&#x6709;&#x6210;&#x5458;&#xFF0C;&#x90FD;&#x88AB;&#x4ECE;&#x90A3;&#x4E2A;&#x70B9;&#x81EA;&#x52A8;&#x589E;&#x52A0;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;<code>Direction.Up</code>&#x7684;&#x503C;&#x4E3A;<code>1</code>&#xFF0C;<code>Down</code>&#x4E3A;<code>2</code>&#xFF0C;<code>Left</code>&#x4E3A;<code>3</code>&#xFF0C;<code>Right</code>&#x4E3A;<code>4</code>&#x3002;</p>
<p>&#x5982;&#x6709;&#x9700;&#x8981;&#xFF0C;&#x4EA6;&#x53EF;&#x5C06;&#x521D;&#x59CB;&#x503C;&#x5B8C;&#x5168;&#x7559;&#x7A7A;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> Direction {
Up,
Down,
Left,
Right,
}
</code></pre>
<p>&#x6B64;&#x65F6;&#xFF0C;<code>Up</code>&#x7684;&#x503C;&#x5C06;&#x4E3A;<code>0</code>&#xFF0C;<code>Down</code>&#x5C06;&#x4E3A;<code>1</code>&#xFF0C;&#x7B49;&#x7B49;&#x3002;&#x5BF9;&#x4E8E;&#x90A3;&#x4E9B;&#x4E0D;&#x4F1A;&#x8003;&#x8651;&#x6210;&#x5458;&#x503C;&#x672C;&#x8EAB;&#x7684;&#x6848;&#x4F8B;&#xFF0C;&#x8FD9;&#x79CD;&#x81EA;&#x52A8;&#x589E;&#x52A0;&#x7684;&#x884C;&#x4E3A;&#x662F;&#x6709;&#x7528;&#x7684;&#xFF0C;&#x4E0D;&#x8FC7;&#x65E0;&#x9700;&#x62C5;&#x5FC3;&#x5728;&#x540C;&#x4E00;&#x679A;&#x4E3E;&#x4E2D;&#x5404;&#x4E2A;&#x503C;&#x4E0E;&#x5176;&#x5B83;&#x503C;&#x662F;&#x5404;&#x5F02;&#x7684;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x679A;&#x4E3E;&#x5F88;&#x7B80;&#x5355;&#xFF1A;&#x53EA;&#x8981;&#x4EE5;&#x679A;&#x4E3E;&#x672C;&#x8EAB;&#x5C5E;&#x6027;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x679A;&#x4E3E;&#x7684;&#x540D;&#x79F0;&#x6765;&#x58F0;&#x660E;&#x7C7B;&#x578B;&#xFF0C;&#x6765;&#x8BBF;&#x95EE;&#x5176;&#x4EFB;&#x4F55;&#x6210;&#x5458;&#x5373;&#x53EF;&#xFF08;Using an enum is simple: just access any member as a property off of the enum itself, and declare types using the name of the enum&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> Response {
No = <span class="hljs-number">0</span>,
Yes,
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">respond</span> (<span class="hljs-params">recipient: <span class="hljs-built_in">string</span>, message: Response</span>): <span class="hljs-title">void</span> </span>{
<span class="hljs-comment">// ...</span>
}
respond (<span class="hljs-string">&quot;Princess Caroline&quot;</span>, Response.Yes);
</code></pre>
<p>&#x6570;&#x5B57;&#x679A;&#x4E3E;&#x53EF;&#x6DF7;&#x5408;&#x8BA1;&#x7B97;&#x7684;&#x4E0E;&#x5E38;&#x91CF;&#x6210;&#x5458;&#xFF08;&#x89C1;&#x540E;&#xFF09;&#x3002;&#x7B80;&#x5355;&#x7684;&#x8BF4;&#xFF0C;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x503C;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF0C;&#x8981;&#x4E48;&#x9700;&#x653E;&#x5728;&#x7B2C;&#x4E00;&#x4E2A;&#xFF0C;&#x6216;&#x5FC5;&#x987B;&#x5728;&#x90A3;&#x4E9B;&#x4EE5;&#x6570;&#x503C;&#x5E38;&#x91CF;&#x6216;&#x5176;&#x5B83;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x521D;&#x59CB;&#x5316;&#x8FC7;&#x7684;&#x6570;&#x5B57;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x4E4B;&#x540E;&#xFF08;Numberic enums can be mixed in computed and constant members(see below). The short story is, enums without initializers either need to be first, or have to come after numberic enums initialized with numberic constants or other constant enum members&#xFF09;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x4E0B;&#x9762;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x662F;&#x4E0D;&#x5141;&#x8BB8;&#x7684;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> E {
A = getSomeValue (),
B, <span class="hljs-comment">// Enum member must have initializer. (1061)</span>
}
</code></pre>
<h2 id="&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#xFF08;string-enums&#xFF09;">&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#xFF08;String enums&#xFF09;</h2>
<p>&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#x7684;&#x6982;&#x5FF5;&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x6709;&#x4E00;&#x4E9B;&#x7EC6;&#x5FAE;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x4E0A;&#x7684;&#x4E0D;&#x540C;&#xFF08;runtime differences&#xFF09;&#xFF0C;&#x540E;&#x9762;&#x4F1A;&#x6709;&#x8BF4;&#x660E;&#x3002;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#x4E2D;&#xFF0C;&#x6BCF;&#x4E2A;&#x6210;&#x5458;&#x90FD;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x503C;&#xFF0C;&#x6216;&#x5176;&#x5B83;&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x52A0;&#x4EE5;&#x521D;&#x59CB;&#x5316;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> Direction {
Up = <span class="hljs-string">&quot;UP&quot;</span>,
Down = <span class="hljs-string">&quot;DOWN&quot;</span>,
Left = <span class="hljs-string">&quot;LEFT&quot;</span>,
Right = <span class="hljs-string">&quot;RIGHT&quot;</span>,
}
</code></pre>
<p>&#x867D;&#x7136;&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#x4E0D;&#x5177;&#x6709;&#x81EA;&#x52A8;&#x589E;&#x52A0;&#x884C;&#x4E3A;&#xFF0C;&#x5B83;&#x4EEC;&#x5374;&#x4ECD;&#x7136;&#x53D7;&#x76CA;&#x4E8E;&#x5176;&#x826F;&#x597D;&#x7684;&#x201C;&#x8FDE;&#x7EED;&#x6027;&#x201D;&#x3002;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;&#xFF0C;&#x52A0;&#x5165;&#x6B63;&#x5728;&#x5BF9;&#x7A0B;&#x5E8F;&#x8FDB;&#x884C;&#x8C03;&#x8BD5;&#xFF0C;&#x800C;&#x4E0D;&#x5F97;&#x4E0D;&#x8BFB;&#x53D6;&#x67D0;&#x4E2A;&#x6570;&#x5B57;&#x679A;&#x4E3E;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x503C;&#xFF0C;&#x8BE5;&#x503C;&#x901A;&#x5E38;&#x662F;&#x4E0D;&#x900F;&#x660E;&#x7684; -- &#x8BE5;&#x503C;&#x5E76;&#x4E0D;&#x80FD;&#x63D0;&#x4F9B;&#x5230;&#x4EFB;&#x4F55;&#x5176;&#x672C;&#x8EAB;&#x6709;&#x7528;&#x7684;&#x610F;&#x4E49;&#xFF08;&#x5C3D;&#x7BA1;&#x53CD;&#x5411;&#x6620;&#x5C04;&#x901A;&#x5E38;&#x6709;&#x6240;&#x5E2E;&#x52A9;&#xFF09;&#xFF0C;&#x4F46;&#x5B57;&#x7B26;&#x4E32;&#x679A;&#x4E3E;&#x5374;&#x5141;&#x8BB8;&#x5728;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x72EC;&#x7ACB;&#x4E8E;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x672C;&#x8EAB;&#xFF0C;&#x8D4B;&#x4E88;&#x6709;&#x610F;&#x4E49;&#x4E14;&#x53EF;&#x8BFB;&#x7684;&#x503C;&#xFF08;While string enums don&apos;t have auto-incrementing behavior, string enums have the benefit that they &quot;serialize&quot; well. In other words, if you are debugging and had to read the runtime value of a numeric enum, the value is ofter opaque - it doesn&apos;t convey any useful meaning on its own(though reverse mapping can often help), string enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member itself&#xFF09;&#x3002;</p>
<h2 id="&#x5F02;&#x8D28;&#x679A;&#x4E3E;&#xFF08;heterogeneous-enums&#xFF09;">&#x5F02;&#x8D28;&#x679A;&#x4E3E;&#xFF08;Heterogeneous enums&#xFF09;</h2>
<p>&#x6280;&#x672F;&#x4E0A;&#x679A;&#x4E3E;&#x662F;&#x53EF;&#x4EE5;&#x6DF7;&#x5408;&#x5B57;&#x7B26;&#x4E32;&#x4E0E;&#x6570;&#x5B57;&#x6210;&#x5458;&#x7684;&#xFF0C;&#x4F46;&#x8FD9;&#x4E48;&#x505A;&#x4F3C;&#x4E4E;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x7406;&#x7531;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> BooleanLikeHeterogeneousEnum {
No = <span class="hljs-number">0</span>,
Yes = <span class="hljs-string">&quot;YES&quot;</span>,
}
</code></pre>
<p>&#x9664;&#x975E;&#x8981;&#x4EE5;&#x67D0;&#x79CD;&#x660E;&#x667A;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5229;&#x7528;JavaScript&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x884C;&#x4E3A;&#xFF0C;&#x5426;&#x5219;&#x5EFA;&#x8BAE;&#x4E0D;&#x8981;&#x8FD9;&#x6837;&#x505A;&#xFF08;Unless you&apos;re really trying to take advantage of JavaScript&apos;s runtime behavior in a clever way, it&apos;s advised that you don&apos;t do this&#xFF09;&#x3002;</p>
<h2 id="&#x8BA1;&#x7B97;&#x7684;&#x4E0E;&#x5E38;&#x91CF;&#x6210;&#x5458;&#xFF08;computed-and-constant-members&#xFF09;">&#x8BA1;&#x7B97;&#x7684;&#x4E0E;&#x5E38;&#x91CF;&#x6210;&#x5458;&#xFF08;Computed and constant members&#xFF09;</h2>
<p>&#x679A;&#x4E3E;&#x7684;&#x6BCF;&#x4E2A;&#x6210;&#x5458;&#xFF0C;&#x90FD;&#x6709;&#x7740;&#x4E00;&#x4E2A;&#x4E0E;&#x5176;&#x5173;&#x8054;&#x7684;&#x503C;&#xFF0C;&#x8BE5;&#x503C;&#x53EF;&#x4EE5;&#x662F; <em>&#x5E38;&#x91CF;&#x6216;&#x8BA1;&#x7B97;&#x503C;(constant or computed)</em>&#x3002;&#x5728;&#x4EE5;&#x4E0B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x5C06;&#x88AB;&#x770B;&#x7740;&#x662F;&#x5E38;&#x91CF;&#xFF1A;</p>
<ul>
<li><p>&#x5176;&#x4F5C;&#x4E3A;&#x679A;&#x4E3E;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x6210;&#x5458;&#x4E14;&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x5176;&#x5C31;&#x88AB;&#x8D4B;&#x4E88;&#x503C;<code>0</code>&#xFF1A;</p>
<pre><code class="lang-typescript"> <span class="hljs-comment">// E.X &#x662F;&#x5E38;&#x91CF;</span>
<span class="hljs-keyword">enum</span> E { X }
</code></pre>
</li>
<li><p>&#x6CA1;&#x6709;&#x521D;&#x59CB;&#x503C;&#xFF0C;&#x4E14;&#x524D;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x662F;&#x4E00;&#x4E2A; <em>&#x6570;&#x5B57;</em> &#x5E38;&#x91CF;&#x3002;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#x5F53;&#x524D;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x503C;&#x5C06;&#x662F;&#x5176;&#x524D;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x52A0;&#x4E00;&#x3002;</p>
<pre><code class="lang-typescript"> <span class="hljs-comment">// `E1`&#x4E0E;`E2`&#x4E2D;&#x7684;&#x6240;&#x6709;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x90FD;&#x662F;&#x5E38;&#x91CF;&#x3002;</span>
<span class="hljs-keyword">enum</span> E1 { X, Y, Z }
<span class="hljs-keyword">enum</span> E2 { A = <span class="hljs-number">1</span>, B, C }
</code></pre>
</li>
<li><p>&#x4EE5;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;a constant enum expression&#xFF09;&#x521D;&#x59CB;&#x5316;&#x7684;&#x6210;&#x5458;&#x3002;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#x662F;TypeScript&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x4E00;&#x4E2A;&#x5B50;&#x96C6;&#xFF0C;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x53EF;&#x88AB;&#x5B8C;&#x6574;&#x6267;&#x884C;&#x3002;&#x5728;&#x6EE1;&#x8DB3;&#x4EE5;&#x4E0B;&#x6761;&#x4EF6;&#x662F;&#xFF0C;&#x8868;&#x8FBE;&#x5F0F;&#x5C31;&#x662F;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;</p>
<ol>
<li>&#x5B57;&#x9762;&#x7684;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x57FA;&#x672C;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x6570;&#x5B57;&#x8868;&#x8FBE;&#x5F0F;, a literal enum expression(basically a string literal or a numeric literal)&#xFF09;</li>
<li>&#x5BF9;&#x5148;&#x524D;&#x5B9A;&#x4E49;&#x7684;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF08;&#x53EF;&#x4EE5;&#x6765;&#x81EA;&#x4E0D;&#x540C;&#x679A;&#x4E3E;&#xFF09;&#x7684;&#x5F15;&#x7528; &#xFF08;a reference to previously defined constant enum member(which can originate from a different enum)&#xFF09;</li>
<li>&#x4E00;&#x4E2A;&#x7528;&#x62EC;&#x53F7;&#x5305;&#x56F4;&#x7684;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;a parentthesized constant enum expression&#xFF09;</li>
<li>&#x8FD0;&#x7528;&#x5230;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#x7684;<code>+</code>&#x3001;<code>-</code>&#x53CA;<code>~</code>&#x4E09;&#x4E2A;&#x4E00;&#x5143;&#x8FD0;&#x7B97;&#x7B26;&#x4E4B;&#x4E00;&#xFF08;one of the <code>+</code>, <code>-</code>, <code>~</code> unary operators applied to constant enum expression&#xFF09;</li>
<li>&#x4E0E;&#x5C06;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x4E3A;&#x64CD;&#x4F5C;&#x6570;&#x4E00;&#x8D77;&#x7684;<code>+</code>&#x3001;<code>-</code>&#x3001;<code>*</code>&#x3001;<code>/</code>&#x3001;<code>%</code>&#x3001;<code>&gt;&gt;</code>&#x3001;<code>&lt;&lt;</code>&#x3001;<code>&gt;&gt;&gt;</code>&#x3001;<code>&amp;</code>&#x3001;<code>|</code>&#x3001;<code>^</code>&#x7B49;&#x4E8C;&#x5143;&#x8FD0;&#x7B97;&#x7B26;</li>
</ol>
</li>
</ul>
<p>&#x5BF9;&#x4E8E;&#x7ED3;&#x679C;&#x4E3A;<code>NaN</code>&#xFF08;Not a Number, &#x975E;&#x6570;&#x503C;&#xFF09;&#x6216;<code>Infinity</code>&#xFF08;&#x65E0;&#x7A77;&#xFF09;&#xFF0C;&#x5C06;&#x4F5C;&#x4E3A;&#x7F16;&#x8BD1;&#x65F6;&#x9519;&#x8BEF;&#x52A0;&#x4EE5;&#x5BF9;&#x5F85;&#xFF08;It is compile time error for constant enum expressions to be evaluated to <code>NaN</code> or <code>Infinity</code>&#xFF09;&#x3002;</p>
<p>&#x90A3;&#x4E48;&#x5176;&#x5B83;&#x6240;&#x6709;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x90FD;&#x5C06;&#x88AB;&#x770B;&#x4F5C;&#x662F;&#x8BA1;&#x7B97;&#x7684;&#xFF08;In all other cases enum member is considered computed&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> FileAccess {
<span class="hljs-comment">// &#x5E38;&#x91CF;&#x6210;&#x5458;</span>
None,
Read = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">1</span>,
Write = <span class="hljs-number">1</span> &lt;&lt; <span class="hljs-number">2</span>,
ReadWrite = Read | Write,
<span class="hljs-comment">// &#x8BA1;&#x7B97;&#x7684;&#x6210;&#x5458;</span>
G = <span class="hljs-string">&quot;123&quot;</span>.length,
}
</code></pre>
<h2 id="&#x8054;&#x5408;&#x679A;&#x4E3E;&#x4E0E;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7C7B;&#x578B;&#xFF08;union-enums-and-enum-member-types&#xFF09;">&#x8054;&#x5408;&#x679A;&#x4E3E;&#x4E0E;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7C7B;&#x578B;&#xFF08;Union enums and enum member types&#xFF09;</h2>
<p>&#x5B58;&#x5728;&#x8FD9;&#x4E48;&#x4E00;&#x4E2A;&#x975E;&#x8BA1;&#x7B97;&#x7684;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x7279;&#x6B8A;&#x5B50;&#x96C6;&#xFF1A; <strong>&#x5B57;&#x9762;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;</strong>&#x3002;&#x5B57;&#x9762;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x662F;&#x4E0D;&#x5E26;&#x6709;&#x521D;&#x59CB;&#x503C;&#x7684;&#xFF0C;&#x6216;&#x6709;&#x7740;&#x88AB;&#x521D;&#x59CB;&#x5316;&#x4E3A;&#x4EE5;&#x4E0B;&#x503C;&#x7684;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF08;There is a special subset of constant enum members that aren&apos;t calculated: literal enum members. <strong>A literal enum member</strong> is a constant enum member with no initialized value, or with values that are initialized to&#xFF09;:</p>
<ul>
<li>&#x4EFB;&#x610F;&#x5B57;&#x7B26;&#x4E32;&#x5B57;&#x9762;&#x503C;&#xFF08;&#x6BD4;&#x5982;<code>&quot;foo&quot;</code>&#x3001;<code>&quot;bar&quot;</code>&#x3001;<code>&quot;baz&quot;</code>&#xFF09;</li>
<li>&#x4EFB;&#x610F;&#x6570;&#x5B57;&#x7684;&#x5B57;&#x9762;&#x503C;&#xFF08;&#x6BD4;&#x5982;<code>1</code>&#x3001;<code>100</code>&#xFF09;</li>
<li>&#x5E94;&#x7528;&#x5230;&#x4EFB;&#x610F;&#x6570;&#x5B57;&#x5B57;&#x9762;&#x503C;&#x7684;&#x4E00;&#x5143;&#x51CF;&#x53F7;&#x8FD0;&#x7B97;&#x7B26;&#xFF08;&#x6BD4;&#x5982;<code>-1</code>&#x3001;<code>-100</code>&#xFF09;</li>
</ul>
<p>&#x5728;&#x67D0;&#x4E2A;&#x679A;&#x4E3E;&#x4E2D;&#x6240;&#x6709;&#x6210;&#x5458;&#x90FD;&#x6709;&#x7740;&#x5B57;&#x9762;&#x679A;&#x4E3E;&#x503C;&#x65F6;&#xFF0C;&#x67D0;&#x4E9B;&#x7279;&#x522B;&#x7684;&#x8BED;&#x6CD5;&#x5C31;&#x4F1A;&#x751F;&#x6548;&#x3002;</p>
<p>&#x7B2C;&#x4E00;&#x5C31;&#x662F;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x8FD8;&#x6210;&#x4E3A;&#x4E86;&#x7C7B;&#x578B;&#xFF01;&#x6BD4;&#x5982;&#xFF0C;&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x8BF4;&#x67D0;&#x4E9B;&#x6210;&#x5458; <em>&#x53EA;</em> &#x80FD;&#x5177;&#x6709;&#x67D0;&#x4E2A;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x503C;&#xFF08;The first is that enum members also become types as well! For example, we can say that certain members can <em>only</em> have the value of an enum member&#xFF09;:</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> ShapeKind {
Circle,
Square,
}
<span class="hljs-keyword">interface</span> Circle {
kind: ShapeKind.Circle;
radius: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">interface</span> Square {
kind: ShapeKind.Square;
sideLength: <span class="hljs-built_in">number</span>;
}
<span class="hljs-keyword">let</span> c: Circle = {
kind: ShapeKind.Square,
<span class="hljs-comment">// Type &apos;{ kind: ShapeKind.Square; radius: number; }&apos; is not assignable to type &apos;Circle&apos;.</span>
<span class="hljs-comment">// Types of property &apos;kind&apos; are incompatible.</span>
<span class="hljs-comment">// Type &apos;ShapeKind.Square&apos; is not assignable to type &apos;ShapeKind.Circle&apos;. (2322)</span>
radius: <span class="hljs-number">100</span>,
}
</code></pre>
<p>&#x53E6;&#x4E00;&#x6539;&#x53D8;&#xFF0C;&#x5C31;&#x662F;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x672C;&#x8EAB;&#xFF0C;&#x6709;&#x6548;&#x5730;&#x6210;&#x4E3A;&#x5404;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684; <em>&#x8054;&#x5408;</em> &#x3002;&#x867D;&#x7136;&#x5230;&#x8FD9;&#x91CC;&#x8FD8;&#x6CA1;&#x6709;&#x8BA8;&#x8BBA;&#x5230; <strong>&#x8054;&#x5408;&#x7C7B;&#x578B;</strong> &#xFF08;<strong>union types</strong>&#xFF09;&#xFF0C;&#x53EA;&#x9700;&#x77E5;&#x9053;&#x6709;&#x4E86;&#x8054;&#x5408;&#x679A;&#x4E3E;&#xFF0C;TypeScript&#x7684;&#x7C7B;&#x578B;&#x7CFB;&#x7EDF;&#xFF0C;&#x5C31;&#x80FD;&#x591F;&#x5229;&#x7528;&#x5176;&#x5BF9;&#x5B58;&#x5728;&#x4E8E;&#x679A;&#x4E3E;&#x672C;&#x8EAB;&#x4E2D;&#x7684;&#x90A3;&#x4E9B;&#x786E;&#x5207;&#x503C;&#x7684;&#x77E5;&#x6089;&#x8FD9;&#x4E00;&#x4E8B;&#x5B9E;&#x3002;&#x800C;&#x6B63;&#x7531;&#x4E8E;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;TypeScript&#x5C31;&#x80FD;&#x6355;&#x6349;&#x5230;&#x90A3;&#x4E9B;&#x53EF;&#x80FD;&#x8FDB;&#x884C;&#x4E0D;&#x6B63;&#x786E;&#x5730;&#x503C;&#x6BD4;&#x8F83;&#x7B49;&#x611A;&#x8822;&#x7A0B;&#x5E8F;&#x9519;&#x8BEF;&#xFF08;The other change is that enum types themselves effectively become a <em>union</em> of each enum member. While we havn&apos;t discussed <strong>union types</strong> yet, all that you need to know is that with union enums, the type system is able to leverage the fact that it knows the exact set of values that exist in the enum itself. Because of that, TypeScript can catch silly bugs where we might be comparing values incorrectly&#xFF09;&#x3002;&#x6BD4;&#x5982;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> E {
Foo,
Bar,
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span> (<span class="hljs-params">x: E</span>) </span>{
<span class="hljs-keyword">if</span> ( x !== E.Foo || x !== E.Bar ) {
<span class="hljs-comment">// ~~~~~~~~~~</span>
<span class="hljs-comment">// Operator &apos;!==&apos; cannot be applied to types &apos;E.Foo&apos; and &apos;E.Bar&apos;. (2365)</span>
}
}
</code></pre>
<p>&#x5728;&#x8BE5;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x9996;&#x5148;&#x68C0;&#x67E5;&#x4E86;<code>x</code>&#x662F;&#x5426;&#x4E0D;&#x662F;<code>E.Foo</code>&#x3002;&#x5982;&#x6B64;&#x68C0;&#x67E5;&#x6210;&#x529F;&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x91CC;&#x7684;<code>||</code>&#x5C06;&#x77ED;&#x8DEF;&#xFF0C;&#x540C;&#x65F6;<code>if</code>&#x7684;&#x8BED;&#x53E5;&#x4F53;&#x5C06;&#x5F97;&#x5230;&#x8FD0;&#x884C;&#x3002;&#x4F46;&#x662F;&#x82E5;&#x90A3;&#x4E2A;&#x68C0;&#x67E5;&#x4E0D;&#x6210;&#x529F;&#xFF0C;&#x90A3;&#x4E48;<code>x</code>&#x5C31;&#x53EA;&#x80FD;&#x662F;<code>E.Foo</code>&#xFF0C;&#x56E0;&#x6B64;&#x518D;&#x6765;&#x5224;&#x65AD;&#x5176;&#x662F;&#x5426;&#x7B49;&#x4E8E;<code>E.Bar</code>&#x5C31;&#x6CA1;&#x6709;&#x610F;&#x4E49;&#x4E86;&#xFF08;In that example, we first checked whether <code>x</code> was <em>not</em> <code>E.Foo</code>. If that check succeeds, then our <code>||</code> will <em>short-circuit</em>, and the body of the <code>if</code> will get run. However, if the check didn&apos;t succed, then <code>x</code> can <em>only</em> be <code>E.Foo</code>, so it doesn&apos;t make sense to see whether it&apos;s equal to <code>E.Bar</code>&#xFF09;&#x3002;</p>
<h2 id="&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x679A;&#x4E3E;&#xFF08;enums-at-runtime&#xFF09;">&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x679A;&#x4E3E;&#xFF08;Enums at runtime&#xFF09;</h2>
<p>&#x8FD0;&#x884C;&#x65F6;&#x5B58;&#x5728;&#x7684;&#x679A;&#x4E3E;&#xFF0C;&#x90FD;&#x662F;&#x771F;&#x5B9E;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x4E0B;&#x9762;&#x7684;&#x8FD9;&#x4E2A;&#x679A;&#x4E3E;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> E {
X, Y, Z
}
</code></pre>
<p>&#x5C31;&#x80FD;&#x88AB;&#x786E;&#x5207;&#x5730;&#x4F20;&#x9012;&#x7ED9;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">f</span>(<span class="hljs-params">obj: { X: <span class="hljs-built_in">number</span> }</span>) </span>{
<span class="hljs-keyword">return</span> obj.X;
}
f(E);
</code></pre>
<h2 id="&#x53CD;&#x5411;&#x6620;&#x5C04;&#xFF08;reverse-mappings&#xFF09;">&#x53CD;&#x5411;&#x6620;&#x5C04;&#xFF08;Reverse mappings&#xFF09;</h2>
<p>&#x9664;&#x4E86;&#x521B;&#x5EFA;&#x51FA;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x5C5E;&#x6027;&#x540D;&#x79F0;&#x6210;&#x5458;&#x7684;&#x5BF9;&#x8C61;&#x4E4B;&#x5916;&#xFF0C;&#x6570;&#x5B57;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x503C;&#x5230;&#x679A;&#x4E3E;&#x540D;&#x79F0;&#x7684; <em>&#x53CD;&#x5411;&#x6620;&#x5C04;</em> &#xFF08;In addition to creating an object with property names for members, numeric enums members also get a <em>reverse mapping</em> from enum values to enum names&#xFF09;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">enum</span> Enum {
A
}
<span class="hljs-keyword">let</span> a = Enum.A;
<span class="hljs-keyword">let</span> nameOfA = Enum[a]; <span class="hljs-comment">// &quot;A&quot;</span>
</code></pre>
<p>TypeScript &#x4F1A;&#x5C06;&#x6B64;&#x7F16;&#x8BD1;&#x5230;&#x7C7B;&#x4F3C;&#x4E0B;&#x9762;&#x7684;JavaScript&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> Enum;
(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">Enum</span>) </span>{
Enum[Enum[<span class="hljs-string">&quot;A&quot;</span>] = <span class="hljs-number">0</span>] = <span class="hljs-string">&quot;A&quot;</span>;
})( Enum || (Enum = {}) );
<span class="hljs-keyword">var</span> a = Enum.A;
<span class="hljs-keyword">var</span> nameOfA = Enum[a]; <span class="hljs-comment">// &quot;A&quot;</span>
</code></pre>
<p>&#x5728;&#x751F;&#x6210;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x679A;&#x4E3E;&#x5C31;&#x88AB;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x4E2A;&#x540C;&#x65F6;&#x5B58;&#x50A8;&#x4E86;&#x6B63;&#x5411;&#xFF08;<code>name</code> -&gt; <code>value</code>&#xFF09;&#x4E0E;&#x9006;&#x5411;&#xFF08;<code>value</code> -&gt; <code>name</code>&#xFF09;&#x6620;&#x5C04;&#x7684;&#x5BF9;&#x8C61;&#x4E2D;&#x3002;&#x5BF9;&#x5176;&#x5B83;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x603B;&#x662F;&#x4F5C;&#x4E3A;&#x5C5E;&#x6027;&#x8BBF;&#x95EE;&#x800C;&#x88AB;&#x7701;&#x7565;&#xFF0C;&#x4E14;&#x7EDD;&#x4E0D;&#x4F1A;&#x88AB;&#x5185;&#x8054;&#xFF08;In this generated code, an enum is compiled into an object that stores both forward (<code>name</code> -&gt; <code>value</code>) and reverse (<code>value</code> -&gt; <code>name</code>) mappings. References to other enum members are always emitted as property accesses and never inlined&#xFF09;&#x3002;</p>
<p>&#x8BF7;&#x8BB0;&#x4F4F;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x7684;&#x53CD;&#x5411;&#x6620;&#x5C04;&#xFF08;Keep in mind that string enum members <em>do not</em> get a reverse mapping generated at all&#xFF09;&#x3002;</p>
<h2 id="&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#xFF08;const-enums&#xFF09;">&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#xFF08;<code>const</code> enums&#xFF09;</h2>
<p>&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x679A;&#x4E3E;&#x90FD;&#x662F;&#x4E00;&#x79CD;&#x76F8;&#x5F53;&#x6709;&#x6548;&#x7684;&#x65B9;&#x6848;&#x3002;&#x4E0D;&#x8FC7;&#x67D0;&#x4E9B;&#x65F6;&#x5019;&#x9700;&#x6C42;&#x66F4;&#x4E3A;&#x7D27;&#x81F4;&#x3002;&#x4E3A;&#x907F;&#x514D;&#x4ED8;&#x51FA;&#x989D;&#x5916;&#x751F;&#x6210;&#x7684;&#x4EE3;&#x7801;&#x4E0E;&#x5728;&#x8BBF;&#x95EE;&#x679A;&#x4E3E;&#x503C;&#x65F6;&#x591A;&#x4F59;&#x7684;&#x95F4;&#x63A5;&#x6027;&#x8FD9;&#x4E24;&#x4E2A;&#x4EE3;&#x4EF7;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x3002;&#x6240;&#x8C13;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#xFF0C;&#x5C31;&#x662F;&#x5728;&#x679A;&#x4E3E;&#x4E0A;&#x4F7F;&#x7528;<code>const</code>&#x8FD9;&#x4E2A;&#x4FEE;&#x9970;&#x5668;&#xFF0C;&#x6240;&#x5B9A;&#x4E49;&#x7684;&#x679A;&#x4E3E;&#xFF08;In most cases, enums are a perfectly valid solution. However sometimes requirements are tighter. To avoid paying the cost of extra generated code and additional indirection when accessing enum values, it&apos;s possible to use <code>const</code> enums. Const enums are defined using the <code>const</code> modifier on our enums&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> <span class="hljs-keyword">enum</span> Enum {
A = <span class="hljs-number">1</span>,
B = A * <span class="hljs-number">2</span>
}
</code></pre>
<p>&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x53EA;&#x80FD;&#x4F7F;&#x7528;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x800C;&#x4E0E;&#x5E38;&#x89C4;&#x679A;&#x4E3E;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x5B83;&#x4EEC;&#x5728;&#x7F16;&#x8BD1;&#x671F;&#x95F4;&#x5C31;&#x88AB;&#x5B8C;&#x5168;&#x79FB;&#x9664;&#x4E86;&#x3002;&#x5728;&#x4F7F;&#x7528;&#x5230;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5176;&#x6210;&#x5458;&#x5B8C;&#x5168;&#x662F;&#x5185;&#x8054;&#x7684;&#x3002;&#x8FD9;&#x53EF;&#x80FD;&#x662F;&#x56E0;&#x4E3A;&#x5E38;&#x91CF;&#x679A;&#x4E3E;&#x4E0D;&#x80FD;&#x62E5;&#x6709;&#x8BA1;&#x7B97;&#x7684;&#x6210;&#x5458;&#x7684;&#x5173;&#x7CFB;&#xFF08;Const enums can only use constant enum expressions and unlike regular enums they are completely removed during compilation. Const enum members are inlined at use sites. This is possible since const enums cannot have computed members&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">const</span> <span class="hljs-keyword">enum</span> Directions {
Up,
Down,
Left,
Right
}
<span class="hljs-keyword">let</span> directions = [Directions.Up, Directions.Down, Directions.Left, Directions.Right];
</code></pre>
<p>&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x6240;&#x5BF9;&#x5E94;&#x7684;&#x7F16;&#x8BD1;&#x751F;&#x6210;&#x7684;JavaScript&#x4EE3;&#x7801;&#x5C06;&#x6210;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> directions = [<span class="hljs-number">0</span> <span class="hljs-comment">/* Up */</span>, <span class="hljs-number">1</span> <span class="hljs-comment">/* Down */</span>, <span class="hljs-number">2</span> <span class="hljs-comment">/* Left */</span>, <span class="hljs-number">3</span> <span class="hljs-comment">/* Right */</span>];
</code></pre>
<h2 id="&#x73AF;&#x5883;&#x679A;&#x4E3E;&#xFF08;ambient-enums&#xFF09;">&#x73AF;&#x5883;&#x679A;&#x4E3E;&#xFF08;Ambient enums&#xFF09;</h2>
<p>&#x73AF;&#x5883;&#x679A;&#x4E3E;&#x7528;&#x4E8E;&#x63CF;&#x8FF0;&#x5DF2;&#x5B58;&#x5728;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x7684;&#x5F62;&#x72B6;&#xFF08;Ambient enums are used to describe the shape of already existing enum types&#xFF09;&#x3002;</p>
<pre><code class="lang-typescript"><span class="hljs-keyword">declare</span> <span class="hljs-keyword">enum</span> Enum {
A = <span class="hljs-number">1</span>,
B,
C = <span class="hljs-number">2</span>
}
</code></pre>
<p>&#x73AF;&#x5883;&#x679A;&#x4E3E;&#x4E0E;&#x975E;&#x73AF;&#x5883;&#x679A;&#x4E3E;&#x7684;&#x4E00;&#x4E2A;&#x91CD;&#x8981;&#x7684;&#x4E0D;&#x540C;&#xFF0C;&#x5C31;&#x662F;&#x5728;&#x5E38;&#x89C4;&#x679A;&#x4E3E;&#x4E2D;&#xFF0C;&#x4E0D;&#x5E26;&#x6709;&#x521D;&#x59CB;&#x5668;&#x7684;&#x6210;&#x5458;&#xFF0C;&#x5728;&#x5176;&#x524D;&#x5BFC;&#x679A;&#x4E3E;&#x6210;&#x5458;&#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x5E38;&#x91CF;&#x65F6;&#xFF0C;&#x5C06;&#x88AB;&#x770B;&#x4F5C;&#x662F;&#x5E38;&#x91CF;&#x3002;&#x800C;&#x4E0E;&#x6B64;&#x76F8;&#x6BD4;&#xFF0C;&#x4E0D;&#x5E26;&#x6709;&#x521D;&#x59CB;&#x5668;&#x7684;&#x73AF;&#x5883;&#xFF08;&#x4E14;&#x975E;&#x5E38;&#x91CF;&#xFF09;&#x679A;&#x4E3E;&#x6210;&#x5458;&#xFF0C; <em>&#x59CB;&#x7EC8;</em> &#x88AB;&#x8BA4;&#x4E3A;&#x662F;&#x8BA1;&#x7B97;&#x7684;&#x6210;&#x5458;&#xFF08;One important difference between ambient and non-ambient enums is that, in regular enums, members that don&apos;t have an initializer will be considered constant if its preceding enum member is considered constant. In contrast, an ambient(and non-const) enum member that does not have initializer is <em>always</em> considered computed&#xFF09;&#x3002;</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="06_generics.html" class="navigation navigation-prev " aria-label="Previous page: 泛型">
<i class="fa fa-angle-left"></i>
</a>
<a href="08_type_inference.html" class="navigation navigation-next " aria-label="Next page: 类型推导">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"枚举","level":"1.8","depth":1,"next":{"title":"类型推导","level":"1.9","depth":1,"path":"08_type_inference.md","ref":"08_type_inference.md","articles":[]},"previous":{"title":"泛型","level":"1.7","depth":1,"path":"06_generics.md","ref":"06_generics.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"sharing":{"weibo":false,"all":["facebook","google","twitter","weibo","instapaper"],"google":false,"twitter":true,"vk":false,"instapaper":false,"facebook":true},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css","pdf":"styles/pdf.css","epub":"styles/epub.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css","pdf":"styles/pdf.css","epub":"styles/epub.css"}},"file":{"path":"07_enums.md","mtime":"2019-04-03T06:43:16.312Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-04-04T00:04:23.982Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>