JavaScript 中的 instanceof 運算子
JavaScript 中的 instanceof 運算子在執行時環境中檢查物件的型別。它返回的結果是布林型別,即如果給定的輸入物件與正在檢查的物件相同,則返回“true”,否則返回“false”。
語法
instanceof 運算子的語法如下所示:
var myVar = nameOfObject instanceof typeOfObject
instanceof 運算子在 JavaScript 中用於一個獨特的目的。在 JavaScript 中,一些變數宣告時沒有提及變數型別或資料型別。在 C、C++、Java 中,必須宣告變數的資料型別。因此,在 JavaScript 中,為了知道宣告的變數的資料型別,我們使用 instanceof 運算子,以便可以知道變數的型別是物件還是任何資料型別。
示例 1
此示例演示了在 JavaScript 中使用instanceof 運算子:
<!DOCTYPE html> <html> <body> <p id="arr1"></p> <p id="TP"></p> <script> var arr = ["Tutorials", "Point", "Articles"]; document.getElementById("arr1").innerHTML = "The given input is: " + arr + " type is: " + typeof(arr) document.getElementById("TP").innerHTML = "Check the instance of given object" + "<br>" + "For Array: " + (arr instanceof Array) + "<br>" + "For Number: " + (arr instanceof Number); </script> </body> </html>
示例 2
以下是此運算子的另一個示例:
<!DOCTYPE html> <html> <body> <p id="arr1"></p> <p id="TP"></p> <script> var departments = ["IT", "CSE", "ECE", "EEE"]; document.getElementById("arr1").innerHTML = "The given input is: " + departments + " and type of: " + typeof(departments) document.getElementById("TP").innerHTML = "Checking the instance of given object with different data types " + "<br>" + "for Array: " + (departments instanceof Array) + "<br>" + "for String: " + (departments instanceof String) + "<br>" + "for Object: " + (departments instanceof Object) + "<br>" + "for Number: " + (departments instanceof Number); </script> </body> </html>
示例 3
使用字串和日期
現在,讓我們看看使用 instanceof 運算子與字串和日期物件。
<!DOCTYPE html> <html> <body> <p id="ds"></p> <p id="TP"></p> <script> var Str = new String(); var Dt = new Date(); document.getElementById("ds").innerHTML = "The given input is: " + Dt + Str + " type of: " + typeof(Dt) + " " + typeof(Str) document.getElementById("TP").innerHTML = "Checking the instance of given objects with different data types: " + "<br>" + "with given Str for Object " + (Str instanceof Object) + "<br>" + "with given Str for Date: " + (Str instanceof Date) + "<br>" + "with given Str for String: " + (Str instanceof String) + "<br>" + "with given Dt for Date: " + (Dt instanceof Date) + "<br>" + "with given Dt for Object: " + (Dt instanceof Object) + "<br>" + "with given Dt for String: " + (Dt instanceof String); </script> </body> </html>
示例 4
使用原始值
字串和數字是原始值,而不是物件,因此沒有 [[Prototype]],所以只有將它們包裝在普通物件中才能工作。
console.log(1 instanceof Number) console.log(new Number(1) instanceof Number) console.log("" instanceof String) console.log(new String("") instanceof String)
示例 5
可建構函式
返回其物件的函式(JS 類)可以使用 instanceof 運算子檢查其物件。
function Person(name) { this.name = name } let john = new Person("John"); console.log(john instanceof Person)
示例 6
繼承
JS 支援原型繼承,因此如果您檢查層次結構中任何類的 instanceof,它將返回 true。
class Person {} class Student extends Person { constructor(name) { super() this.name = name } } let john = new Student("John"); console.log(john instanceof Person) console.log(john instanceof Student)
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP