TypeScript 中的 Symbol 型別


Symbol 是在 JavaScript 的最新主要版本 ES6 中引入的。Symbol 是一種資料型別。就像我們使用數字、字串或布林值來建立不同資料型別的變數一樣,我們可以使用 Symbol 型別來建立 Symbol。

使用 Symbol 型別有很多好處,因為它提供了比其他資料型別更多的功能。在本教程中,我們將學習 Symbol 的基礎知識及其不同用途。

語法

使用者可以按照以下語法建立 Symbol 資料型別的變數。

let test_symbol = Symbol();
let key_symbol = Symbol("key-for-symbol"); 

在上述語法中,我們使用 Symbol() 建構函式建立了 Symbol。我們還可以將鍵作為 Symbol 引數傳遞以識別 Symbol。

TypeScript中,我們可以建立多個唯一的 Symbol。這意味著 Symbol 是不可變的;即使我們使用相同的鍵建立 Symbol,我們也可以發現這兩個 Symbol 都是唯一的。

// Creating two different symbols with the same key.
var first_sym = Symbol("sym");
var second_sym = Symbol("sym");

// we can't compare two symbols as they are immutable
if (first_sym === second_sym) {
}
else {

   // execution flow always executes this block
}

在上面的程式碼中,使用者可以看到它會產生編譯錯誤,因為這兩個 Symbol 是不可比較的。

示例 1

在下面的示例中,我們建立了 Symbol 並使用了 Symbol 的 description 屬性來獲取我們在建立 Symbol 時作為 Symbol 引數傳遞的值。

此外,我們使用了 toString() 方法將 Symbol 轉換為字串。

// Creating the symbols
const first_sym = Symbol("sym");
const second_sym = Symbol("sym");

//  getting the description of the symbol
console.log("The description of the first_sym is " + first_sym.description);

// converting symbols to string
console.log("The symbol in the string is " + first_sym.toString());

編譯後,它將在 JavaScript 中生成相同的程式碼。

輸出

它將產生以下輸出:

The description of the first_sym is undefined
The symbol in the string is Symbol(sym) 

示例 2:使用 Symbol 定義物件屬性

在下面的示例中,我們建立了 Symbol 並定義了物件。我們使用 obj_symbol 作為物件的屬性。此外,我們可以像訪問物件的普通屬性一樣訪問它。

const obj_symbol = Symbol();

// creating the object
let object = {

   // using the obj_symbol as key of object
   [obj_symbol]: "obj value",
};

// accessing the symbol property of the object
console.log(
   "The vlaue of the obj_symbol property in the object is " + object[obj_symbol]
);

編譯後,它將在 JavaScript 中生成相同的程式碼。

輸出

它將產生以下輸出:

The vlaue of the obj_symbol property in the object is obj value

在 switch case 語句中使用 Symbol

由於每個 Symbol 都是唯一的,因此我們可以將其用作 switch-case 語句的 case。當我們在 switch case 語句中使用 Symbol 時,它確保每個 case 都是唯一的。如果任何 case 與作為 switch 語句引數傳遞的 case 不匹配,則它將轉到 default case。

語法

使用者可以按照以下語法在 switch case 語句中使用 Symbol 型別。

switch(symbol) {
   case symbol1: 
      break
   case symbol2: 
      break;
}

在上述語法中,一個 Symbol 作為 switch 語句的引數傳遞。之後,我們使用 Symbol 名稱後跟 case 關鍵字來建立不同的 case。

示例

在下面的示例中,我們建立了四個不同的 Symbol。之後,我們定義了 print_symbol 函式,其中包含 switch case 語句來處理不同的 case。

在 switch case 語句中,我們使用 Symbol 值作為 case 並執行特定 case 的程式碼。

// different symbols
const symbol1 = Symbol();
const symbol2 = Symbol();
const symbol3 = Symbol();
const symbol4 = Symbol();

function print_symbol(symbol) {

   // creating the switch case statement
   switch (symbol) {
   
      // different cases
      case symbol1:
         console.log("The case is symbol 1.");
         break;
      case symbol2:
         console.log("The case is symbol 2.");
         break;
      case symbol3:
         console.log("The case is symbol 3.");
         debugger;
         break;
      case symbol4:
         console.log("The case is symbol 4.");
         break;
      default:
         console.log("The case is default.");
   }
}

// calling the function
print_symbol(symbol2);
print_symbol(symbol4);

編譯後,它將在 JavaScript 中生成相同的程式碼。

輸出

它將產生以下輸出:

The case is symbol 2.
The case is symbol 4.

唯一 Symbol

TypeScript中,Symbol 是一種原始資料型別。因此,我們只需要將其用作型別,但我們也可以使用“唯一 Symbol”將其用作字面量。Symbol 包括唯一 Symbol,這意味著唯一 Symbol 是 Symbol 的子型別。

我們只能將唯一 Symbol 用於 const 變數和只讀屬性。如果我們想將特定的 Symbol 型別引用到另一個變數,我們可以使用“typeof”運算子。

語法

使用者可以按照以下語法使用 Symbol 作為字面量使用唯一 Symbol。

const test_symbol: unique symbol = Symbol();
let symbol1: typeof test_symbol = test_symbol;
class C {
   static readonly symb: unique symbol = Symbol("unique symbol");
}

示例

在下面的示例中,我們聲明瞭型別為 Symbol 的 test_symbol 並使用 unique symbol 關鍵字將 Symbol 用作型別字面量。此外,使用者可以觀察我們如何使用 typeof 運算子將 Symbol 用作使用 let 和 var 關鍵字宣告的變數的型別字面量。

此外,我們使用 unique symbol 關鍵字定義了只讀靜態類的成員的型別。

// here, we used the unique symbol to define the type of the sym1.
const test_symbol: unique symbol = Symbol();

// we can't reference the unique symbol to the let type of variable
// let sym2: unique symbol = Symbol();

// to reference the symbol type to the variables declared with the let keyword, using the typeof operator.
let symbol1: typeof test_symbol = test_symbol;
console.log("The value of symbol1 is " + typeof test_symbol);

// referencing the unique symbol to the static class property
class C {
   static readonly symb: unique symbol = Symbol("unique symbol");
}

編譯後,它將生成以下 JavaScript 程式碼:

// here, we used the unique symbol to define the type of the sym1.
var test_symbol = Symbol();

// we can't reference the unique symbol to the let type of variable

// let sym2: unique symbol = Symbol();

// to reference the symbol type to the variables declared with the let keyword, using the typeof operator.
var symbol1 = test_symbol;
console.log("The value of symbol1 is " + typeof test_symbol);

// referencing the unique symbol to the static class property
var C = /** @class */ (function () {
   function C() {
   }
   C.symb = Symbol("unique symbol");
   return C;
}());

輸出

以上程式碼將產生以下輸出:

The value of symbol1 is symbol

在本教程中,我們學習了 Symbol 型別的基礎知識。此外,我們學習瞭如何使用“unique symbol”關鍵字將 Symbol 型別用作型別字面量。此外,我們學習瞭如何使用 typeof 運算子獲取另一個變數的 Symbol 型別並將其用作另一個變數的型別。

更新於:2023年1月17日

5K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.