javascript之typeof、instanceof操作符使用探討_基礎知識
寫javascirpt程式碼時,typeof和instanceof這兩個運算子時不時就會用到,堪稱必用。但是!使用它們總是不能直接的得到想要的結果,非常糾結,普遍的說法認為“這兩個操作符或許是javascript中最大的設計缺陷,因為幾乎不可能從他們那裡得到想要的結果”
typeof
說明:typeof回傳一個表達式的資料類型的字串,傳回結果為js基本的資料類型,包括number,boolean,string,object,undefined,function。
從說明來看,看起來沒什麼問題。
下面的程式碼寫了一個數值變量,typeof後的結果是"number"。
var a = 1; typeof(a)); //=>number
console.log(typeof(a)); //=>object
但是!問題就在於既然呼叫了typeof就應該要準確地傳回一個變數的類型,不管是直接用值創建的還是用類型的構造函數創建的,否則!我還用你做啥!
那麼對於:
a和b變數的型別準確的說來都應該是Number才是想要的結果。
取得類型資訊:
console.log(Object.prototype.toString.call(a));
console.log(Object.prototype.toString.call(b ));
輸出:
複製程式碼
程式碼如下:
var a = 1;
var b = new Number(1);
console.log( Object.prototype.toString.call(a).slice(8,-1));
console.log(Object.prototype.toString.call(b).slice(8,-1));
複製程式碼
程式碼如下:
function is(obj,type) {
複製程式碼
程式碼如下:
var a1=1;
var a2=Number(1 );
var b1="hello";
var b2=new String("hello");
var c1=[1,2,3];
var c2=new Array(1 ,2,3);
console.log("a1's typeof:" typeof(a1));
console.log("a2's typeof:" typeof(a2));
console.log(" b1's typeof:" typeof(b1));
console.log("b2's typeof:" typeof(b2));
console.log("c1's typeof:" typeof(c1));
console .log("c2's typeof:" typeof(c2));
輸出:
The newly created function we use is:
console.log("a1 is Number:" is(a1,"Number"));
console.log("a2 is Number:" is(a2,"Number"));
console.log( "b1 is String:" is(b1,"String"));
console.log("b2 is String:" is(b2,"String"));
console.log("c1 is Array :" is(c1,"Array"));
console.log("c2 is Array:" is(c2,"Array"));
Output:
a1 is Number:true
a2 is Number:true
b1 is String:true
b2 is String:true
c1 is Array:true
c2 is Array:true
Note: typeof It is not useless. The actual use is to detect whether a variable has been defined or assigned a value.
instanceof
Description: Determine whether an object is a certain data type, or whether a variable is an instance of an object.
The instanceof operator is also powerless when used to compare two built-in type variables, and will also be dissatisfied with the result.
console.log("abc" instanceof String); / / false
console.log("abc" instanceof Object); // false
console.log(new String("abc") instanceof String); // true
console.log(new String( "abc") instanceof Object); // true
Reflects the relationship accurately only when comparing custom objects.
function Person() {}
function Man( ) {}
Man.prototype = new Person();
console.log(new Man() instanceof Man); // true
console.log(new Man() instanceof Person); // true

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

instanceof的作用是判斷一個物件是否是某個類別的實例,或者是否實作了某個介面。 instanceof是一個用來檢查物件是否為指定類型的運算子。 instanceof運算子使用場景:1、類型檢查:可以用來判斷一個物件的具體類型,以便根據不同類型執行不同的邏輯;2、介面判斷:可以用來判斷一個物件是否實現了某個接口,以便根據介面的定義呼叫對應的方法;3、向下轉型等等。

在不同的shell中,使用’!’符號的大多數Linux命令用法可能會有所不同。雖然我提供的範例通常在bashshell中使用,但其他一些Linuxshell可能具有不同的實現,或者可能根本不支援某些對’!’符號的使用。讓我們深入了解Linux命令中’!’符號的令人驚奇和神秘的用法。 1.使用指令編號從歷史記錄中執行指令你可能不知道的是,你可以從歷史指令中執行一個指令(已經執行過的指令)。首先,透過執行’history’指令找到指令的編號。 linuxmi@linuxmi:~/www.linuxmi.

模等於運算符(%)在PHP中是一個非常常用的運算符,用於計算兩個數相除的餘數。在本文中,我們將深入了解模等於操作符的用法,並提供具體的程式碼範例幫助讀者更好地理解。首先,讓我們來看一個簡單的例子,假設我們需要計算一個數除以另一個數的餘數:$a=10;$b=3;$remainder=$a%$b;echo"10除以3的餘數是:&

sql in操作符使用:1、單列匹配,可以使用IN操作符匹配一個列中的多個值;2、多列匹配,IN操作符也可以用於匹配多個列的值;3、子查詢, IN操作符也可以與子查詢一起使用,子查詢是一個嵌套在主查詢中的查詢語句。

概念1、此運算子用於操作物件的例子,檢查物件是否為特定類型(類型或介面類型)。格式2、如果計算器左側變數所指的對像是操作器右側類別或介面的對象,則結果是真實的。 (Objectreferencevariable)instanceof(class/interfacetype)實例packagecom.verify_instanceof;publicclassTestInstanceOf{publicstaticvoidmain(String[]args){//下方四行程式碼用來證明:instanceof

在Java中,instanceof是一個二元運算符,用於檢查一個物件是否是一個類別的實例,或者是一個類別的子類別的實例,其語法形式為“object instanceof class”,其中,object是一個對象引用,class是一個類別名稱或介面名稱。

instanceof是JavaScript 中的一個操作符,用於檢測構造函數的”prototype“屬性是否出現在對象的原型鏈中的任何位置,語法為”object instanceof constructor“,其中object是要檢測的對象,constructor是要進行檢查的構造函數。

不用instanceof的原因有:1、正在使用的程式語言可能不支援instanceof運算符,;2、認為使用其他方法可以更好地實現需求,在某些情況下,使用其他方法來檢查物件類型可能更有效或更適合你的需求;3、不熟悉instanceof運算子的使用方式或不確定它的行為;4、在某些情況下,使用"instanceof" 可能不是最佳的選擇。
