typeof與instanceof操作符

2021-08-10 12:09:42 字數 1571 閱讀 8348

typeof與instanceof都是用來檢測變數型別的,不同的是,typeof用來檢測基本資料型別,instanceof用來檢測引用型別

一 . typeof操作符

typeof操作符是確定乙個變數是字串、數值、布林值、還是undefined的最佳工具

對乙個值使用typeof操作符可能返回下列某個字串

1.」undefined」—— 如果這個值未定義

var message;

console.log(typeof message); //

"undefined"

2.」boolean」—— 如果這個值是布林值

console.log(typeof

true); //

"boolean"

console.log(typeof

false); //

"boolean"

3.」string」—— 如果這個值是字串

console.log(typeof

'123'); //

"string"

4.」number」—— 如果這個值是數值

console.log(typeof

123.123); //

"number"

console.log(typeof

123); //

"number"

5.」object」—— 如果這個值是物件或null

console.log(typeof

null); //

"object"

var obj = new object();

console.log(typeof obj); //

"object"

6.」function」—— 如果這個值是函式

var func = function

() ;

console.log(typeof func); // "function"

二 . instanceof

instanceof可以檢測乙個物件是什麼型別的物件

如果變數是給定引用型別的例項,那麼instanceof操作符就會返回true

function

person

(name, job)

var person = new person("john", "doctor");

alert(person instanceof person); // true

alert(person instanceof

object); // true

根據規定,所有引用型別的值都是object物件的例項。因此在檢測乙個引用型別和object建構函式時,instanceof操作符始終會返回true,如果使用instanceof操作符檢測基本型別的值,則該操作符始終會返回false

(完)

JS基礎之typeof和instanceof用法

在js中當不確定運算元的型別時,可以通過typeof 函式返回變數的型別。typeof 函式會把型別資訊當做字串返回,且typeof的返回值有六種情況,這六種返回值型別分別是 typeof的使用 舉例說明 console.log typeof null object console.log type...

Pipe類與Instance類介紹

pipe類與instance類介紹 章芝青 抽象類pipe,是所有pipe類的父類,這些類的作用就是將資料從一種形式轉成另一種形式。在特徵提取階段,pipe類是最常用的。pipe類操縱instance類,instance類是描述資料的類,也是十分重要的乙個類。每乙個instance包括四個預定義的域...

instance恢復概覽

instance恢復概覽 相關動態檢視 oracle 伺服器提供許多標準檢視以獲取有關資料庫和例程的資訊。這些檢視包括 v sga 查詢有關例程的以下各項的大小 共享池 日誌緩衝區 資料緩衝區快取記憶體以及固定記憶體大小 取決於作業系統 v instance 查詢例程的狀態,如例程模式 例程名 啟動...