1.列举 JavaScript 中的类型,有什么类型判断的方法?
"原生类型"
Boolean
Number
Null
Undefined
Symbol
String
Bigint
"引用类型"
Object
最常见的判断方法:typeof
console.log(typeof 1) // number
console.log(typeof 'test') // string
console.log(typeof true) // boolean
console.log(typeof undefined) // undefined
console.log(typeof null) // object
console.log(typeof {}) // object
console.log(typeof []) // object
console.log(typeof Symbol()) // symbol
判断已知对象类型的方法:instanceof
instanceof操作符用于检查一个对象是否书友某个特定的class,同时它还考虑了继承.A instanceof B
表示判断A是否位B的实例,如果是返回true
instanceof 只能用来判断两个对象是否属于实例关系, 而不能判断一个对象实例具体属于哪种类型。
- 根据对象的constructor判断
A.constructor === B
在实际情况下,constructor属性可以被随意修改,而且你在原型继承中,很容易忽略调constructor的指向
function Rabbit (name) {
Animal.call(this,name);
}
Rabbit.prototype = Object.create(Animal.prototype);
//需要说动设置construct的正确指向
Rabbit.prototype = constructor = Rabbit;
const rabbit = new Rabbit('小兔');
rabbit.contructor === Rabbit //true
- 通用方法: prototype
toString()
方法能识别是因为引擎为它们设置好了toStringTag
标签
console.log(Object.prototype.toString.call('')) // [object String]
console.log(Object.prototype.toString.call(1)) // [object Number]
console.log(Object.prototype.toString.call(true)) // [object Boolean]
console.log(Object.prototype.toString.call(Symbol())) //[object Symbol]
console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
console.log(Object.prototype.toString.call(null)) // [object Null]
console.log(Object.prototype.toString.call(new Function())) // [object Function]
console.log(Object.prototype.toString.call(new Date())) // [object Date]
console.log(Object.prototype.toString.call([])) // [object Array]
console.log(Object.prototype.toString.call(new RegExp())) // [object RegExp]
console.log(Object.prototype.toString.call(new Error())) // [object Error]
console.log(Object.prototype.toString.call({})) // [object Object]
console.log(Object.prototype.toString.call(Math)) // [object Math]
console.log(Object.prototype.toString.call(JSON)) // [object JSON]
function a () {
console.log(Object.prototype.toString.call(arguments)) // [object Arguments]
}
a()
class ValidatorClass {}
let newClass = new ValidatorClass();
console.log(Object.prototype.toString.call(newClass));[object Object]
而自己创建的类却不会,toString()
找不到toStringTag
属性只好返回Object
标签
万能方法:
jquery.type()
//如果对象是undefined或null,则返回相应的“undefined”或“null”。
jQuery.type( true ) === "boolean"
jQuery.type( 3 ) === "number"
jQuery.type( "test" ) === "string"
jQuery.type( function(){} ) === "function"
jQuery.type( [] ) === "array"
jQuery.type( new Date() ) === "date"
jQuery.type( new Error() ) === "error" // as of jQuery 1.9
jQuery.type( /test/ ) === "regexp"
2.结合类型判断的各种方法,手写一个
getType
函数参数:value,任何类型,一个值
- 返回值:String 类型,value 的类型
- 要求:
null => ‘null’
Boolean 原始值 => ‘boolean’
Boolean 对象(new Boolean(true))=> ‘Boolean’
特殊对象(Math)=> ‘Math’
function getType(value) {
let get = Object.prototype.toString.call(value).replace('[object ','').replace(']','')
//如果是引用类型的直接返回,是原始类型的返回小写
if(typeof value === 'object') return get;
else return get.toLowerCase()
}
console.log(getType(true));
- 模拟 instanceof
function instanceOf(object, constructor) {
if(typeof object !== 'object' || constructor === 'null') return false;
//拿到原型
let proto = Object.getPrototypeOf(object)
//顺着原型链去找,直到找到相同的原型对象,返回true,否则为false
while(1) {
if(proto === null) return false;
if(proto === constructor.prototype) return true;
proto = Object.getPrototypeOf(proto)
}
}
- 阐述 Object.prototype.toString.call().slice(8,-1) 原理
- toString()方法,返回对象的字符串表现
- 数组,方法等Object的实例,都各自重写了toString方法
- 要得到对象的具体类型,需要调用Object的原型的未被重写的toString方法
- call的原因是调转指针,将被call的指向Object.prototype指向的(子项通过call调用了原父项的toString方法)