js类型判断:typeof与instanceof
1、js中的数据类型
基本数据类型:Undefined、Null、Boolean、Number、String,Symbol
引用数据类型 :Object
let bool = true;
let num = ;
let str = 'abc';
let und= undefined;
let nul = null;
let arr = [,,,];
let obj = {name:'xiaoming',age:};
let fun = function(){console.log('hello')};
let s1 = Symbol();
2、typeof
console.log(typeof bool); //boolean
console.log(typeof num);//number
console.log(typeof str);//string
console.log(typeof und);//undefined
console.log(typeof nul);//object
console.log(typeof arr);//object
console.log(typeof obj);//object
console.log(typeof fun);//function
console.log(typeof s1); //symbol
所以typeof可以用来识别一些基本类型。
3、instanceof
console.log(bool instanceof Boolean);// false
console.log(num instanceof Number);// false
console.log(str instanceof String);// false
console.log(und instanceof Object);// false
console.log(nul instanceof Object);// false
console.log(arr instanceof Array);// true
console.log(obj instanceof Object);// true
console.log(fun instanceof Function);// true
console.log(s1 instanceof Symbol);// false
但是可以检测出引用类型,如array、object、function,同时对于是使用new声明的类型,它还可以检测出多层继承关系。
其实也很好理解,js的继承都是采用原型链来继承的。比如objA instanceof A ,其实就是看objA的原型链上是否有A的原型,而A的原型上保留A的constructor属性。
所以instanceof一般用来检测对象类型,以及继承关系。
4、constructor
console.log(bool.constructor === Boolean);// true
console.log(num.constructor === Number);// true
console.log(str.constructor === String);// true
console.log(arr.constructor === Array);// true
console.log(obj.constructor === Object);// true
console.log(fun.constructor === Function);// true
console.log(s1.constructor === Symbol);//true
null、undefined没有construstor方法,因此constructor不能判断undefined和null。
但是他是不安全的,因为contructor的指向是可以被改变。
5、Object.prototype.toString.call
console.log(Object.prototype.toString.call(bool));//[object Boolean]
console.log(Object.prototype.toString.call(num));//[object Number]
console.log(Object.prototype.toString.call(str));//[object String]
console.log(Object.prototype.toString.call(und));//[object Undefined]
console.log(Object.prototype.toString.call(nul));//[object Null]
console.log(Object.prototype.toString.call(arr));//[object Array]
console.log(Object.prototype.toString.call(obj));//[object Object]
console.log(Object.prototype.toString.call(fun));//[object Function]
console.log(Object.prototype.toString.call(s1)); //[object Symbol]
此方法可以相对较全的判断js的数据类型。
至于在项目中使用哪个判断,还是要看使用场景,具体的选择,一般基本的类型可以选择typeof,引用类型可以使用instanceof。
6、综合:
JavaScript 标准文档只给出了一种获取 [[Class]] 值的方法,那就是使用 Object.prototype.toString。
function is(type, obj) {
var clas = Object.prototype.toString.call(obj).slice(8, -1);//slice() 参数8过滤[object ,参数-1,过滤]
return obj !== undefined && obj !== null && clas === type;
} is('String', 'test'); // true
is('String', new String('test')); // true
上面例子中,Object.prototype.toString 方法被调用,this 被设置为了需要获取 [[Class]] 值的对象。
Object.prototype.toString 返回一种标准格式字符串,所以上例可以通过 slice 截取指定位置的字符串,如下所示:
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call(2) // "[object Number]"
js类型判断:typeof与instanceof的更多相关文章
- JS类型判断typeof PK {}.toString.call(obj)
参考链接:https://www.talkingcoder.com/article/6333557442705696719 先看typeof <!doctype html> <htm ...
- JavaScript 类型判断 —— typeof 以及 instanceof 中的陷阱
JavaScript中基本类型包含Undefined.Null.Boolean.Number.String以及Object引用类型.基本类型可以通过typeof来进行检测,对象类型可以通过instan ...
- 浅谈JS中的typeof和instanceof的区别
JS中的typeof和instanceof常用来判断一个变量是否为空,或者是什么类型. typeof typeof运算符返回一个用来表示表达式的数据类型的字符串. typeof一般返回以下几个字符串: ...
- JS基础-数据类型判断typeof、instanceof、Object.prototype.toString
typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型. Object.proto ...
- 类型和原生函数及类型转换(二:终结js类型判断)
typeof instanceof isArray() Object.prototype.toString.call() DOM对象与DOM集合对象的类型判断 一.typeof typeof是一个一元 ...
- js类型判断-丰富加好用
一, 自己有时候写一些东西,要做类型判断,还有测试的时候,对于原生的和jQuery中的类型判断,实在不敢恭维,所以就写了一个好用的类型判断,一般情况都够用的. function test(type) ...
- JS类型判断&原型链
JS类型检测主要有四种 1.typeof Obj 2.L instanceof R 3.Object.prototype.toString.call/apply(); 4.Obj.constructo ...
- 看jquery3.3.1学js类型判断的技巧
需要预习:call , typeof, js数据类型 1. isFunction中typeof的不靠谱 源码: var isFunction = function isFunction( obj ) ...
- js类型判断
console.log('---------------------'); var a="string"; console.log(a); //string var a=1; co ...
随机推荐
- [转载]边框回归(Bounding Box Regression)
[转载]边框回归(Bounding Box Regression) 许多模型中都应用到了这种方法来调整piror使其和ground truth尽量接近,例如之前自己看过的SSD模型 这篇文章写的很好, ...
- JavaScript 标准内置对象
JavaScript 标准内置对象或称全局的对象(global objects)不要和 全局对象(global object)混淆.这里说的全局的对象是说在全局作用域里的对象,全局作用域包含了全局对象 ...
- 安卓开发之SimpleAdapter的使用
package com.lidaochen.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundl ...
- Java并发编程之线程池及示例
1.Executor 线程池顶级接口.定义方法,void execute(Runnable).方法是用于处理任务的一个服务方法.调用者提供Runnable 接口的实现,线程池通过线程执行这个 Runn ...
- git 常用命令操作
目录 一.用户和邮箱 用户和邮箱的作用 查看用户名和邮箱地址 修改用户名和邮箱地址 用户名和邮箱地址的作用 用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用 ...
- XXX_initcall()函数分析
1. 先看这些宏的定义(定义在文件include/linux/init.h中) #define pure_initcall(fn) __define_initcall("0",fn ...
- C# Winfrom TabControl美化
实例一: using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; na ...
- 使用 visual studio 2019 社区版打开touchgfx工程注意项
@2019-09-23 [环境] touchgfx designer 4.10.0 visual studio 2019 社区版 [问题] #error 1 使用 visual studio 2019 ...
- Error creating bean with name 'xxxx' defined in URL
遇到这种情况,要检查一下以下配置: 1) service接口实现类上有没有加@Service注解,注解是不是引用的spring的类?不要导错包 2) 接口有没有写实现类,实现类是实现的对应接口么?比如 ...
- JS用正则替换特殊字符
'abcabce'.replace(/abc/g,'') "e" 'abcabce$E$'.replace(/$E$/g,'') "abcabce$E$" 'a ...