typeof 與 instanceof的簡單的應用

2021-09-23 10:31:25 字數 1441 閱讀 9984

typeof 運算子把型別資訊當作字串返回。typeof 返回值有六種可能: 「number,」 「string,」 「boolean,」 「object,」 「function,」 和 「undefined.」

我們可以使用typeof來獲取乙個變數是否存在

示例:

console.

log(

typeof arr == integer)

;// numbers

typeof

37===

'number'

;typeof

3.14

==='number'

;typeof(42

)===

'number'

;// strings

typeof

"bla"

==='string'

;// booleans

typeof

true

==='boolean'

;// undefined

typeof undefined ===

'undefined'

;// functions

typeof

function()

==='function'

;typeof

class

c===

'function'

;typeof math.sin ===

'function'

;

對於array,null等特殊物件使用typeof一律返回object,這正是typeof的侷限性。

如果我們希望獲取乙個物件是否是陣列,或判斷某個變數是否是某個物件的例項則要選擇使用instanceof。

instanceof用於判斷乙個變數是否某個物件的例項,instanceof 左運算元是乙個類,右運算元是標識物件的類。

instanceof 運算子是用來測試乙個物件是否在其原型鏈原型建構函式的屬性。其語法是object instanceof constructor

console.

log(arr instanceof

array);

var a=

newarray()

;alert

(a instanceof

array);

//true,

alert

(a instanceof

object

)//true

function

test()

;var a=

newtest()

;alert

(a instanceof

test

)//true。

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 查詢例程的狀態,如例程模式 例程名 啟動...