2018-08-14  本文已影响0人  张德超

typeof

typeof运算符把类型信息当作字符串返回。有6种可能:"number" ,"string", "boolean", "object", "function" 和 "undefined"

typeof 100 //number

typeof NaN //number

typeof true //boolean

typeof  "hello world" //string

typeof {} //object

typeof [] //object

typeof parseInt //function

typeof hello //undefined

typeof可以准确判断除了object以外的基础数据类型,但不能区分object类型的具体类型,如Date, Array,以及自定义类型。

instanceof

instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上。

function Sup(){}

function Sub(){

  Sup.call(this)

}

Sub.prototype = new Sup()

Sub.prototype.constructor = Sub

var child = new Sub()

console.log(child instanceof Sub)//true

console.log(child instanceof Sup)//true

constructor

object.prototype.toString.call(type)

上一篇 下一篇

猜你喜欢

热点阅读