JavaScript基础(2-2)数据类型检测
分类:javascript
typeof :检测数据类型的运算符,返回的结果是字符串
返回的结果是字符串,字符串中的内容证明了这个值是属于什么类型的
基础数据类型
除了typeof null
是'object'
以外,typeof
都是自己的数据类型
引用数据类型
分为:对象数据类型
和函数数据类型
对象数据类型
分为:{}普通对象型
,[]数组
, /^$/正则
,日期对象
等对象数据类型
和null
的typeof
都是‘object’
函数数据类型
的typeof
都是‘function’
局限性:
typeof null 不是'null' 而是 'object'
:因为null
虽然是单独的一个数据类型,但是它原本意思是空对象指针
,浏览器用typeof
检测 的时候会把它当做对象来检测
- 使用
typeof
无法具体细分出 到底是数组
还是正则
,因为返回结果 都是'object'
面试题:
typeof typeof [] => 'string'
//1、typeof [] =》 'object'
//2、typeof 'object' =》 'string'
typeof 12 => 'number'
var num=13;
typeof num => 'number'
typeof 'aaa'=> 'string'
typeof true => 'boolean'
typeof null => 'object' //typeof 的bug
typeof undefined => 'undefined'
typeof {aaa:'aaa'} => 'object'
typeof [] => 'object'
typeof /^$/ => 'object'
typeof function (){} => 'function'
XX instanceof YY
,检测当前实例是否属于某个类
function Fn(){
}
var f=new Fn();
console.log(f instanceof Fn) //=>true
[] instanceof Array true
[] instanceof Object true
console.log([] instanceof Array) //=>true
console.log(/^$/ instanceof Fn) //=>false
[] instanceof Object //true
/^$/ instanceof Object //true
/^$/ instanceof Array //false
[] instanceof Array //true
/^$/ instanceof RegExp //true
function aaa(){}
console.log(aaa instanceof Function) //true
手动实现instanceof
instanceof 用于判断某个对象是否是另一个对象(构造方法)的实例。
instanceof会查找原型链,直到null如果还不是后面这个对象的实例的话就返回false,否则就返回true
function instanceofMy(obj, constructor) {
// 错误判断 构造函数必须是一个function 其他的均报错
if (typeof constructor !== 'function') throw new Error('instance error')
if (!obj || (typeof obj !== 'object' && typeof obj !== 'function')) return false
// 获取到原型对象
let proto = constructor.prototype
// 如果obj的原型对象不是null
while (obj.__proto__) {
if (obj.__proto__ === proto) return true
obj = obj.__proto__
}
return false
}
console.log(instanceofMy(() => {}, Function)) // true
xxx.constructor: 获取当前实例的构造器
var test=1;
//var test=[1];
//var test='1';
if (test.constructor==Array)
{
console.log("This is an Array");
}
if (test.constructor==Boolean)
{
console.log("This is an Boolean");
}
if (test.constructor==Date)
{
console.log("This is an Date");
}
if (test.constructor==String)
{
console.log("This is an String");
}
if (test.constructor==Number)
{
console.log("This is an Number");
}
var test=[1];
console.log(test.constructor == Object); //false
function employee(name,job,born) {
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
console.log(bill.constructor);
//结果
function employee(name,job,born) {
this.name=name;
this.job=job;
this.born=born;
}
Object.prototype.toString.call : 获取当前实例的所属类信息
Object.prototype.toString
方法,判断某个对象之属于哪种内置类型。分为
null
、string
、boolean
、number
、undefined
、array
、function
、object
、date
、math
判断基本类型:
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call("abc");// "[object String]"
Object.prototype.toString.call(123);// "[object Number]"
Object.prototype.toString.call(true);// "[object Boolean]"
判断原生引用类型:
//**函数类型**
Function fn(){
console.log(“test”);
}
Object.prototype.toString.call(fn); // "[object Function]"
//**日期类型**
var date = new Date();
Object.prototype.toString.call(date); // "[object Date]"
//**数组类型**
var arr = [1,2,3];
Object.prototype.toString.call(arr); // "[object Array]"
//**正则表达式**
var reg = /[hbc]at/gi;
Object.prototype.toString.call(reg); // "[object RegExp]"
//判断原生JSON对象
var isNativeJSON = window.JSON && Object.prototype.toString.call(JSON);
console.log(isNativeJSON);// 输出结果为”[object JSON]”说明JSON是原生的,否则不是;
//**自定义类型**
function Person(name, age) {
this.name = name;
this.age = age;
}
var person = new Person("Rose", 18);
Object.prototype.toString.call(person); // "[object Object]" //能准确判断person是Person类的实例
对于我们自己定义的类型
比如 function Fn() {},则需要设置 知名符号 toStringTag来设置
Fn.prototype[Symbol.toStringTag] = 'Fun'
Object.prototype.toString.call(new Fn()) // "[object Fun]"