如何檢查 JavaScript 變數是否為函式型別?
在本教程中,我們將學習檢查 **JavaScript 變數** 是否為 **函式** 型別 的不同方法。在 JavaScript 中,函式包含程式碼塊,使程式碼重用性更好。
主要有兩種方法可以宣告函式,一種是 **命名函式**,另一種是 **匿名函式**。在宣告匿名函式時,我們需要將其儲存在某個變數中,以便從程式碼中的某個地方訪問它或呼叫該函式。
在這裡,使用者可以看到如何宣告 **匿名** 函式並將其儲存到變數中。
let tutorialspoint = function() { //code for the function}
如上例所示,'tutorialspoint' 變數包含函式,有時程式設計師需要檢查它是否為函式型別。
為了解決上述問題,我們可以使用以下不同的方法。
- 使用 typeof 運算子
- 使用 instanceof 運算子
- 使用 object.prototype.toString 方法
使用 typeof 運算子
在 JavaScript 中,可以使用 **typeof** 運算子確定任何變數的資料型別。它以字串格式返回其運算元的資料型別的值。
在這裡,我們可以使用 JavaScript 中的字串相等 **'==='** 運算子來比較返回值與字串 'function'。如果返回值與 'function' 匹配,則該變數為函式型別。
使用者可以按照以下語法使用 typeof 運算子。
語法
typeof operand typeof(operand)
引數
運算元 - 它可以是任何物件或變數,我們需要檢查它是否為函式型別。
返回值 - 以字串格式返回 '運算元' 的資料型別的值。
示例 1
以下示例演示瞭如何使用 typeof 運算子檢查變數是否為函式型別。
<html> <head> <title>Check type of variable is of function</title> </head> <body> <h2> typeof Operator: Check if a variable is of function type. </h2> <div id = "result"></div> <script type="text/javascript"> let a = 10; let b = 20; var tutorialsPoint = function () { return a + b; }; // function to check type of variable is function or not function checkTypeOfVar() { if (typeof tutorialsPoint === 'function') { document.write("<br>The type of <i>tutorialsPoint</i> variable is function." ); } else { document.write("The type of <i>tutorialsPoint</i> variable is not a function."); } } document.getElementById("result").innerHTML = "var tutorialsPoint = " + tutorialsPoint; // call the function checkTypeOfVar(); </script> </body> </html>
在上面的程式碼中,使用者可以看到 typeof tutorialsPoint 返回 'function',函式的控制權進入 'if' 塊並給出上述輸出。
使用 instanceof 運算子
**'instanceof'** 運算子幫助我們確定任何變數是否為給定型別。它接受兩個運算元,一個變數和另一個數據型別、物件、函式或任何要與當前變數型別進行比較的內容。
在我們的例子中,我們將採用函式作為運算元,因為我們想要檢查變數是否為函式型別。它返回布林值,如果變數的型別與給定型別匹配,則返回 true;否則返回 false。
語法
variable instanceof datatype
在上述語法中,作為資料型別,我們將採用 **'Function'**,因為我們需要驗證 變數 型別是否為函式。如果變數為 Function 型別,則返回 true。
示例 2
在下面的示例中,我們使用 instanceof 運算子檢查變數的函式型別。
<html> <head> <title>Check type of variable is of function</title> </head> <body> <h2> <i>instanceof</i> Operator: check if a variable is of function type. </h2> <div id = "result"></div> <script type="text/javascript"> let a = 10; let b = 20; var multiply = function () { return a * b; // other code for the function }; // function to check type of variable is function or not function checkTypeOfVar() { let isTypeOfFunction = multiply instanceof Function; if (isTypeOfFunction===true) { document.write("<br>The type of <i>multiply</i> variable is <b>function</b>."); } else { document.write("<br>The type of multiply variable is not a function."); } } document.getElementById("result").innerHTML = "var multiply = " + multiply ; // call the function checkTypeOfVar(); </script> </body> </html>
在上面的程式碼中,使用者可以看到 instanceof multiply Function 返回“true”,函式的控制權進入 'if' 塊,並列印上述輸出。
使用 object.prototype.toString 方法
通常,當物件需要在 JavaScript 中返回字串時,會自動為每個物件呼叫 **object.prototype.toString** 方法。此外,如果我們不覆蓋 **object.toString()** 方法的預設返回值,我們可以覆蓋 **toString** 方法的返回值,則為 '[Object 型別]',其中型別為物件型別。
在這裡,我們將檢查它是否返回 'Function' 型別。
獲取任何物件或變數型別的標準語法如下。
語法
Object.prototype.toString.call(Object_Name) == '[ object Function]'
在上述語法中,我們必須在 Object_Name 的位置傳遞變數以檢查它是否為函式型別。
引數
Object_Name - 要檢查的變數,以確定它是否為函式型別。
示例 3
在下面的示例中,我們使用 Object.prototype.toString 方法獲取變數的型別。
<html> <head> <title>Check type of variable is of function</title> </head> <body> <h2><i>object.prototype.toString()</i> Methodd </h2> <div id = "result"></div> <script type="text/javascript"> let a = 10; let b = 20; var substract = function () { return a - b; // other code for the function }; // function to check type of variable is function or not function checkTypeOfVar() { // get the type of tutorialsPoint variable let typeOfVar = Object.prototype.toString.call( substract ); // compare typeOfVar with the funciton type if (typeOfVar == '[object Function]') { document.write("<br>The type of <i>substract </i> variable is <b>function</b>."); } else { document.write("<br>The type of substract variable is not a function."); } } document.getElementById("result").innerHTML = "var substract = " + substract ; // call the function checkTypeOfVar(); </script> </body> </html>
在上面的程式碼中,使用者可以看到 Object.prototype.toString.call(subtract) 方法返回“[object Function]”。因此,它列印上述輸出。
結論
在本教程中,我們看到了三種檢查變數是否為函式例項的方法。使用者可以根據自己的舒適度使用任何一種方法。第一種和第二種方法具有簡單的語法來查詢變數型別,因此使用者可能在使用第三種方法之前嘗試這些方法。