typeof 判断变量的类型
typeof返回值对应 |
类型 | 返回值 |
String | "string" |
Number | "number" |
Boolean | "boolean" |
undefined | "undefined" |
Object | "object" |
function | "function" |
Symbol | "symbol" |
- typeof 的不足之处:
1、不能区分对象、数组、正则返回的都是“object”
2、Safar,Chrome7之前的版本对正则对象返回的是“function”
3、在IE6、7、8中,大多数的宿主对象是对象,而不是函数 如:typeof alert
注释:宿主对象:window、document,内置对象:Math、Date等;本地对象: 就是自己申明的对象
instanceof判断实例对象的构造函数是谁
- 实现原理:实例对象的__proto__这个属性,和构造函数的prototype这个属性是不是同一个引用
- 作用: 判断实例对象的构造函数是谁
function Student(name){
this.name = name;
}
var stu = new Student('张珊')
console.log(stu instanceof Student);
console.log(stu instanceof Object)
in 判断某个对象中是否由某个属性
function Student(name){
this.name = name;
}
Student.prototype.study = function () {
console.log(this.name)
}
var stu = new Student('张珊')
console.log(stu)
console.log("toString" in stu)
console.log("study" in stu)
Object.prototype.toString.call()判断类型
类型 | 返回值 |
String | "[object String]" |
Number | "[object Number]" |
Boolean | "[object Boolean]" |
undefined | "[object Undefined]" |
Object | "[object Object]" |
Array | "[object Array]" |
function | "[object Function]" |
Symbol | "[object Symbol]" |