您的位置:首页 > Web前端 > JavaScript

JavaScript笔记-判断数据类型

2018-03-27 18:30 274 查看

判断数据类型【转】

// 下面几个可以检测出准确的类型

typeof 3;                    // 'number'
typeof NaN;                  // 'number'
typeof '3';                  // 'string'
typeof '';                   // 'string'
typeof true;                 // 'boolean'
typeof Boolean(false);       // 'boolean'
typeof undefined;            // 'undefined'
typeof {};                   // 'object'
typeof function fn() {};     // 'function'

// ES6中的Symbol类型
typeof Symbol();              // 'symbol'
// ES6中的类本质上还是函数
typeof class C {};            // 'function'

// 以下均不能检测出准确的类型
typeof null;                 // 'object'
typeof new Number(3);        // 'object'
typeof new String('3');      // 'object'
typeof new Boolean(true);    // 'object'
typeof [];                   // 'object'
typeof /\w+/;                // 'object'
typeof new Date();           // 'object'
typeof new Error();          // 'object'

// ES6中的Map和Set
typeof new Map();            // 'object'
typeof new Set();            // 'object'


contructor

function is(value, type) {
// 先处理null和undefined
if (value == null) {
return value === type;
}
// 然后检测constructor
return value.constructor === type;
}

var isNull = is(null, null);                  // true
var isUndefined = is(undefined, undefined);   // true
var isNumber = is(3, Number);                 // true
var isString = is('3', String);               // true
var isBoolean = is(true, Boolean);            // true

var isSymbol = is(Symbol(), Symbol);          // true

var isObject = is({}, Object);                // true
var isArray = is([], Array);                  // true
var isFunction = is(function(){}, Function);  // true
var isRegExp = is(/./, RegExp);               // true
var isDate = is(new Date, Date);              // true
var isError = is(new Error, Error);           // true

var isMap = is(new Map, Map);                 // true
var isSet = is(new Set, Set);                 // true
function Animal() {}

var animal = new Animal();

var isAnimal = is(animal, Animal);          // true

Instanceof

var isAnimal = tiger instanceof Animal;      // true
// 虽然typeof null的结果为'object' 但它并不是Object的实例

null instanceof Object;               // false

// 对于基础类型 instanceof操作符并不会有隐式包装

3 instanceof Number;                  // false

'3' instanceof Number;                // false

true instanceof Boolean;              // false

Symbol() instanceof Symbol;           // false

// 只对对象类型起作用

new Number(3) instanceof Number;      // true

new String('3') instanceof String;    // true

new Boolean(true) instanceof Boolean; // true

Object(Symbol()) instanceof Symbol;   // true

({}) instanceof Object;               // true

[] instanceof Array;                  // true

(function(){}) instanceof Function;   // true

/./ instanceof RegExp;                // true

new Date instanceof Date;             // true

new Error instanceof Error;           // true

new Map instanceof Map;               // true

new Set instanceof Set;               // true

Object#toString()

// 利用Object#toString()检测类型

var _toString = Object.prototype.toString;

function is(value, typeString) {
// 获取到类型字符串
var stripped = _toString.call(value).replace(/^\[object\s|\]$/g, '');
return stripped === typeString;
}

is(null, 'Null');               // true
is(undefined, 'Undefined');     // true
is(3, 'Number');                // true
is(true, 'Boolean');            // true
is('hello', 'String');          // true
is(Symbol(), 'Symbol');         // true
is({}, 'Object');               // true
is([], 'Array');                // true
is(function(){}, 'Function');   // true
is(/\w+/, 'RegExp');            // true
is(new Date, 'Date');           // true
is(new Error, 'Error');         // true
is(new Map, 'Map');             // true
is(new Set, 'Set');             // true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript 面试 基础