首頁 web前端 js教程 javascript之typeof、instanceof操作符使用探討_基礎知識

javascript之typeof、instanceof操作符使用探討_基礎知識

May 16, 2016 pm 05:33 PM
instanceof typeof 操作符

寫javascirpt程式碼時,typeof和instanceof這兩個運算子時不時就會用到,堪稱必用。但是!使用它們總是不能直接的得到想要的結果,非常糾結,普遍的說法認為“這兩個操作符或許是javascript中最大的設計缺陷,因為幾乎不可能從他們那裡得到想要的結果”
typeof
說明:typeof回傳一個表達式的資料類型的字串,傳回結果為js基本的資料類型,包括number,boolean,string,object,undefined,function。
從說明來看,看起來沒什麼問題。

下面的程式碼寫了一個數值變量,typeof後的結果是"number"。

複製程式碼 程式碼如下:

var a = 1; typeof(a)); //=>number

如果用Number型別的建構子new一個變數的話,typeof後的結果是"object"。

複製程式碼 程式碼如下:
var a = new Number(1);
console.log(typeof(a)); //=>object

上面的這兩個輸出結果看似沒啥問題,這一點從書上看來是理所當然的事情,因為javascript就是這樣設計的。

但是!問題就在於既然呼叫了typeof就應該要準確地傳回一個變數的類型,不管是直接用值創建的還是用類型的構造函數創建的,否則!我還用你做啥!
那麼對於:

複製程式碼 程式碼如下:
var a = 1; 🎜>var b = new Number(1);


a和b變數的型別準確的說來都應該是Number才是想要的結果。
而準確的類型資訊保存在變數的內部屬性 [[Class]] 的值中,透過使用定義在 Object.prototype 上的方法 toString來取得。

取得類型資訊:


複製程式碼 程式碼如下:
var a = 1;
var b = new Number(1);
console.log(Object.prototype.toString.call(a));
console.log(Object.prototype.toString.call(b ));


輸出:


複製程式碼 程式碼如下:
程式碼如下:


程式碼如下:

程式碼🎜>[object Number] [object Number] 是不是已經很直接了,我們稍微處理一下,得到直接結果:


複製程式碼


程式碼如下:


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));
輸出: Number Number 這就是想要的結果。 為了更好的使用,我們封裝一個方法,用來判斷某個變數是否是某種型別:


複製程式碼


程式碼如下:


function is(obj,type) {
var clas = Object.prototype.toString.call(obj).slice(8, -1); return obj !== undefined && obj !== null && clas === type; } 定義一些變數做過測試,先來看看它們的typeof輸出:


複製程式碼


程式碼如下:


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));
輸出:
a1's typeof:number a2's typeof:object b1's typeof:string c1's 型態of:object c2's typeof:object
The newly created function we use is:
Copy the code The code is as follows:

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.
Copy code The code is as follows:

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.
Copy code The code is as follows:

function Person() {}
function Man( ) {}
Man.prototype = new Person();
console.log(new Man() instanceof Man); // true
console.log(new Man() instanceof Person); // true
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1318
25
PHP教程
1269
29
C# 教程
1248
24
instanceof有什麼作用 instanceof有什麼作用 Nov 14, 2023 pm 03:50 PM

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

Linux 指令中「!」操作符的八個神秘用途 Linux 指令中「!」操作符的八個神秘用途 Jun 27, 2023 pm 12:51 PM

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

深入了解PHP中的模等於運算符的用法 深入了解PHP中的模等於運算符的用法 Mar 19, 2024 pm 12:54 PM

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

sql in運算子使用 sql in運算子使用 Aug 04, 2023 pm 03:58 PM

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

java中instanceof運算子怎麼使用 java中instanceof運算子怎麼使用 May 19, 2023 am 08:16 AM

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

java中instanceof是什麼意思 java中instanceof是什麼意思 Nov 13, 2023 pm 01:52 PM

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

instanceof是什麼意思 instanceof是什麼意思 Nov 20, 2023 pm 02:32 PM

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

為什麼不用instanceof 為什麼不用instanceof Nov 14, 2023 pm 04:05 PM

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

See all articles