类型检验小结
null 空对象也是对象,就是它是空。
这里不是很明白,toString.apply指的是什么?
typeof 返回字符串 基本类型+函数
instanceof 判断对象类型
空对象和空对象不相等
类型检测
typeof 适合基本类型及function 检测,遇到null失效
instanceof 用来检测原型对象
Object.prototype.toString
类型检测小结
typeof 适合 函数对象 和 基本类型的判断
类型检测小结
typeof:适合基本类型及function检测,遇到NULL失效。
[[Class]]:通过{}.toString拿到,适合内置对象和基元类型,遇到null和undefined失效(IE678等返回[object.Object])。
instanceof:适合自定义对象,也可以用来检测原生对象,在不同iframe和window间检测时失效。
//下面进行类型检测的情况,就是说有五种方法,
undefined
//1、首先是第一种方法
undefined
typeof 100
"number"
typeof trueLog
"function"
typeof [1,2]
"object"
typeof (1,23)
"number"
typeof NaN
"number"
typeof null
"object"
//这里面就会奇怪为什么这个null会和object相同,这里面是如果null的类型是null的话会出现大量的问题,所以由于历史原因,就改成object
undefined
//2、但是当我们想判断的更加细节一点,比如说到底是什么object呢,这个时候我们就会用到obj instanceof Object
undefined
//这里面要求了只能判断object这个,基本格式就是obj instanceof Object (它的作用就是左边这个对象的原型链上是否有右边这个构造函数的属性)
undefined
[1,2] instanceof Array
true
//下面介绍一种特殊的
undefined
function Person(){}
undefined
function Student(){}
undefined
Student.prototype = new Person();
Person {}__proto__: constructor: ƒ Person()__proto__: constructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()
Student.prototype.constructor = Student
ƒ Student(){}
function Student(){}
undefined
var bos = new Student()
undefined
bos instanceof Student
true
var one = new Person()
undefined
one instanceof Person
true
one instanceof Student
false
//这个是因为其实student下面有一个prototype叫做person
undefined
//3、object.prototype.toString.apply([]);
undefined
Object.prototype.toSti
undefined
Object.prototype.toString.apply([])
"[object Array]"
//但是上面要注意到在IE678中的null是object的类型
undefined
Object.prototype.toString.apply(function()[])
VM10959:1 Uncaught SyntaxError: Unexpected token '['
//4、还有就是constructor这个查询方式,这个其实是一个指向原型的一个构造器,是任何一个对象都有的,但是它是可以被改写的
undefined
//5、duck type:
undefined
//前三者的区别
undefined
//1、typeof比较适合基本类型的判断,但是要注意的就是null会返回为object,但是可以用严格等于的方式来判断是不是null
undefined
//2、另外就是判断对象用instanceof ,还有就是
undefined
Object.prototype.toString.apply([1])
"[object Array]"
也就是说这里面有三种查看元素类型的方式,一个是typeof,一个是instanceof还有一个prototype原型属性
js类型检测:
1、typeOf在判断基本类型和函数对象非常方便。问题在于不能判断更深层的Object类型,譬如:数组类型、null类型
2、判断对象类型,更常用的是 obj instanceof Object。其底层原理是:判断左边的变量对象的原型链中是否有右边的构造函数的prototype属性
3、Object.prototype.toString.apply(),能判断出Array、Funtion、Null、Undefined。但是ie8以下不兼容
类型检测:typeof ; Object.prototype.toString; instanceof
typeof:基础类型函数对象,遇到null失效
instanceof:判断对象类型(不同window或iframe间的对象检测不能使用instanceof)
Object.prototype.toString,apply():
IE6/7/8Object.prototype.toString.apply(null)返回[object Object]
constructor
duck type
总结
typeof 用于检测基本类型和function 但是不能检测null,遇到null会返回object
object.prototype.toString() 用来判断基本类型、数组、函数,但是null和undefined在IE8以下会存在兼容性问题
instanceof 用来判断对象是都为某个原生对象构造器所构造的,是否继承自某个原生对象,但是在window和iframe上可能失效
Object.prototype,toString() 判断引用类型如下图
instanceof 判断对象类型
obj instanceof Object 左右操作树都应该是对象,如果不是直接返回false
类型判断
typeof -- 可判断基本类型,如下图
instanceof
类型检测小结
由于历史的原因,
typeof null === "object"
null是一个类型,并且只有null这一个值.
数组array也是对象
检测类型的方法:
typeof
instanceof
Object.prototype.toString
constructor
duck type
三种类型检测小结
toString 应用在null和undefined上 会返回[object Null] 和 [object Undefined]
视频解说JavaScript是如何"继承"的
注意这里蓝色[第三行]的:
Student.prototype = new Person()
new Person 出的对象(地址)有一个__proto__属性 被赋予了Person.prototype这个对象地址(我猜的 即__proto__ = Person.prototype)
obj instanceof Object
基于原型链去判断一个标识(zhi)符
instanceof 左边期待是一个对象类型 如果不是 直接返回false 没有隐式转换.
各种类型的typeof结果
常用三种类型检测
Object.prototype.toString.apply()判断数据类型