如何在 JavaScript 中檢查變數或物件的型別?
JavaScript 是一種弱型別程式語言,這意味著沒有宣告變數型別的規則。一個變數可以在程式中儲存多種資料型別,因此在使用變數之前瞭解變數型別至關重要。在 JavaScript 中,我們可以使用 `typeof` 運算子來檢查變數或物件的型別。`typeof` 運算子接受一個變數並返回其型別的字串格式。
除了 `typeof` 運算子之外,JavaScript 還提供 `instanceof` 運算子來檢查變數或物件的型別。`instanceof` 運算子接受兩個引數:要檢查的物件和要檢查型別的建構函式。如果建構函式是物件的型別,則運算子將返回 true。
使用 typeof 運算子
`typeof` 運算子是一個一元運算子,它接受一個引數並返回一個字串,指示該引數的型別。例如,`typeof` 運算子可以用來檢查變數或物件的型別。
語法
typeof variable_name
在上面的語法中,`variable_name` 是您想要確定其型別的變數的名稱。
`typeof` 運算子可以返回以下字串之一:
"number" 用於數字
"string" 用於字串
"boolean" 用於布林值
"undefined" 用於未定義的值
"object" 用於物件(包括陣列和函式)
"symbol" 用於符號(ECMAScript 2015 中新增)
示例
在這個例子中,我們使用 `typeof` 運算子來檢查 JavaScript 中變數或物件的型別。我們聲明瞭不同型別的多個變數,例如數字、字串、布林值等。我們在網頁上顯示了這些變數。我們在按鈕上使用了點選事件處理程式來檢查變數的型別。使用者每次點選按鈕時,都可以在網頁上看到所有變數及其各自的型別。`typeof` 運算子有助於在執行特定操作之前確定變數或物件的型別。例如,您可能使用它來確保變數在執行算術運算之前是數字,或者變數在與另一個字串連線之前是字串。
<html> <body> <h2>Checking the <i> type of a variable or object </i> in JavaScript</h2> <h4>The variables are as follows:</h4> <ul> <li>let num = 10</li> <li>let str = "Hello"</li> <li>let bool = true</li> <li>let un</li> <li>let n = null</li> <li>let arr = [1, 2, 3]</li> <li>let func = function () {}</li> </ul> <button onclick = "checkType()"> Check Type </button> <div id = "root"> </div> <script> let num = 10 let str = 'Hello' let bool = true let un let n = null let arr = [1, 2, 3] let func = function () {} let root = document.getElementById('root') function checkType() { root.innerHTML = '<h4>The types of variables are as follows:</h4>' root.innerHTML += '<ul> <li> let num = 10 is a ' + typeof num + ' </li> <li> let str = "Hello" is a ' + typeof str + ' </li> <li> let bool = true is a ' + typeof bool + ' </li> <li> let un is a ' + typeof un + ' </li> <li> let n = null is a ' + typeof n + ' </li> <li> let arr = [1, 2, 3] is a ' + typeof arr + ' </li> <li> let func = function () {} is a ' + typeof func + ' </li> </ul> ' } </script> </body> </html>
使用 instanceof 運算子
在 JavaScript 中,`instanceof` 運算子用於在執行時確定物件的型別。它返回一個布林結果,指示該物件是否是特定類的例項。
語法
object_name instanceof object_constructor
在上面的語法中,`object_name` 是您想要確定其型別的物件的名稱。
示例
在這個例子中,我們使用 `instanceof` 運算子來檢查 JavaScript 中變數或物件的型別。我們聲明瞭一個使用 String 類建構函式的字串型別變數和一個自定義類物件“myClassObject”,它是“myClass”的物件,並在網頁上顯示它們。我們在按鈕上使用了點選事件處理程式來檢查物件的型別,並在網頁上顯示它們。
<html> <body> <h2>Checking the <i> type of a variable or object </i> in JavaScript</h2> <h4>The object variables are as follows:</h4> <ul> <li>let str = new String('Hello World!')</li> <li>let myClassObject = new MyClass()</li> </ul> <button onclick = "checkType()"> Check Type </button> <div id = "root"> </div> <script> let str = new String('Hello World!') class MyClass {} let myClassObject = new MyClass() let root = document.getElementById('root') function checkType() { root.innerHTML = '<h4> The types of objects using instanceof operator: </h4>' root.innerHTML += '<ul> <li> str is an instance of String: ' + (str instanceof String) + ' </li> <li> str is an instance of MyClass: ' + (str instanceof MyClass) + ' </li> </ul>' root.innerHTML += ' <ul> <li> myClassObject is an instance of String: ' + (myClassObject instanceof String) + ' </li> <li> myClassObject is an instance of MyClass: ' + (myClassObject instanceof MyClass) + ' </li> </ul>' } </script> </body> </html>
當與某些物件一起使用時,`typeof` 和 `instanceof` 運算子可能並非總是返回預期結果。例如,`typeof` 運算子對於陣列返回“object”,即使它們是 JavaScript 中的一種物件型別。要正確檢查值是否為陣列,可以使用 `Array.isArray()` 方法。