如何檢查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>在上面的程式碼中,使用者可以看到`multiply instanceof Function`返回“true”,函式的控制流進入'if'塊並列印上述輸出。
使用`object.prototype.toString`方法
通常,當物件需要在JavaScript中返回字串時,會自動為每個物件呼叫**`object.prototype.toString`**方法。此外,如果我們不覆蓋`object.toString()`方法的預設返回值,我們也可以覆蓋`toString`方法的返回值,預設返回值是'[Object Type]',其中type是物件型別。
在這裡,我們將檢查它是否返回'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]”。所以,它列印了上述輸出。
結論
在本教程中,我們已經看到了三種檢查變數是否為函式例項的方法。使用者可以根據自己的方便程度使用任何一種方法。第一種和第二種方法具有直接的語法來查詢變數型別,因此使用者可能在使用第三種方法之前會選擇它們。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP