- Hands-On Full Stack Web Development with Angular 6 and Laravel 5
- Fernando Monteiro
- 213字
- 2021-07-23 19:18:50
Using enum
enum allows us to group values with more intuitive names. Some people prefer to call the enumerated list and some other names. Let's look at an example, so as to make it easier to understand how this works in practice:
- Create a file called enum.js in the chapter-02 folder, and add the following code:
enum bands {
Motorhead,
Metallica,
Slayer
}
console.log(bands);
- In your Terminal, type the following command to transpile the file:
tsc enum.ts
- Now, let's execute the file. Type the following command:
node enum.js
You will see the following result in the Terminal:
{ '0': 'Motorhead',
'1': 'Metallica',
'2': 'Slayer',
Motorhead: 0,
Metallica: 1,
Slayer: 2 }
We can now get the value by using the name instead of the position.
- Add the following lines of code, right after the console.log() function:
let myFavoriteBand = bands.Slayer;
console.log(myFavoriteBand);
Now, execute the commands in steps 2 and step 3 to check the results. You will see the following output in the Terminal:
{ '0': 'Motorhead',
'1': 'Metallica',
'2': 'Slayer',
Motorhead: 0,
Metallica: 1,
Slayer: 2 }
My Favorite band is: Slayer
Note that all of the values (band names) declared in the band object are converted into strings, inside an indexed object, as you can see in the preceding example.
推薦閱讀
- EJB 3.1從入門到精通
- FreeSWITCH 1.2
- MERN Quick Start Guide
- HTML5 Game development with ImpactJS
- Spring Cloud微服務(wù)架構(gòu)進(jìn)階
- Unity Artificial Intelligence Programming
- Learning Windows 8 Game Development
- 網(wǎng)絡(luò)空間全球治理觀察
- LwIP應(yīng)用開發(fā)實(shí)戰(zhàn)指南:基于STM32
- Selenium WebDriver 3 Practical Guide
- 物聯(lián)網(wǎng)M2M開發(fā)技術(shù):基于無線CPU-Q26XX
- 互聯(lián)網(wǎng)視覺設(shè)計(jì)(全彩慕課版)
- 物聯(lián)網(wǎng)系統(tǒng)設(shè)計(jì)
- 移動應(yīng)用開發(fā)技術(shù)
- 互聯(lián)網(wǎng)下一站:5G與AR/VR的融合