Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

判断js数据类型 #25

Open
aermin opened this issue Feb 22, 2018 · 0 comments
Open

判断js数据类型 #25

aermin opened this issue Feb 22, 2018 · 0 comments
Labels

Comments

@aermin
Copy link
Owner

aermin commented Feb 22, 2018

如果让你判断一个数据是否为数组,你会怎么判断呢?

方法有很多,简单粗暴的比如判断这个数据有没有 length 属性,还有常规的比如typeof ,instanaceof ,Object.prototype.toString.call

typeof

typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined

image

缺点: 对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。

instanaceof

instanceof适用于检测对象,它是基于原型链运作的。

instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。换种说法就是如果左侧的对象是右侧对象的实例, 则表达式返回true, 否则返回false 。

[1, 2, 3] instanceof Array // true 
/abc/ instanceof RegExp // true 
({}) instanceof Object // true 
(function(){}) instanceof Function // true

缺点:instanceof对基本数据类型检测不起作用,主要是因为基本数据类型没有原型链。

Object.prototype.toString.call

Object.prototype.toString.call可以检测各种数据类型,推荐使用。

Object.prototype.toString.call([]); // => [object Array] 
Object.prototype.toString.call({}); // => [object Object] 
Object.prototype.toString.call(''); // => [object String] 
Object.prototype.toString.call(new Date()); // => [object Date] 
Object.prototype.toString.call(1); // => [object Number] 
Object.prototype.toString.call(function () {}); // => [object Function] 
Object.prototype.toString.call(/test/i); // => [object RegExp] 
Object.prototype.toString.call(true); // => [object Boolean] 
Object.prototype.toString.call(null); // => [object Null] 
Object.prototype.toString.call(); // => [object Undefined]

当然,使用的时候记得在使用前定义类型而非在括号内直接定义类型。

function Animal () {}; 
Object.prototype.toString.call (Animal); // => [object Function] 
Object.prototype.toString.call (new Animal); // => [object Object]

造isArray等isType语法糖

var isString = function( obj ){ return Object.prototype.toString.call( obj ) === '[object String]'; }; 

var isArray = function( obj ){return Object.prototype.toString.call( obj ) === '[object Array]'; }; 

var isNumber = function( obj ){return Object.prototype.toString.call( obj ) === '[object Number]'; };

优化:这些函数的大部分实现都是相同的, 不同的只是 Object.prototype.toString. call( obj )返回的字符串。为了避免多余的代码,我们尝试把这些字符串作为参数提前值入 isType 函数。

var isType = function( type ){ 
    return function( obj ){ 
        return Object.prototype.toString.call( obj ) === '[object '+ type +']'; 
    } 
};

var isString = isType( 'String' ); 
var isArray = isType( 'Array' ); 
var isNumber = isType( 'Number' );

console.log( isArray( [ 1, 2, 3 ] ) ); //true
@aermin aermin changed the title 判断数据类型以及实现isType语法糖 判断js数据类型 Feb 22, 2018
@aermin aermin added the js label Feb 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant