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学习——判断数据类型总结(转)
- JavaScript学习笔记——数据类型
- javascript中的真假值、数据类型判断以及+的特殊用法
- javascript -- 判断是否为某个数据类型
- < 笔记 > JavaScript - 03 JavaScript 数据类型
- javascript中判断对象数据类型的方法
- javascript学习笔记-数据类型
- JavaScript 系列笔记(一)数据类型
- 判断JavaScript的数据类型
- javascript数据变量类型判断(JS变量是否是数组,是否是函数的判断)
- 在JavaScript中,判断数据(数组)类型
- JavaScript判断数据类型总结
- JavaScript学习笔记-- Boolean 数据类型
- JavaScript判断数据类型总结
- JavaScript数据类型及判断方法
- 【笔记】【MDN】JavaScript指南 > 语法和数据类型
- javaScript 笔记 (二)变量 和 数据类型
- Javascript笔记——Javascript数据类型转换
- 【JavaScript学习笔记】2:数据类型
- [学习笔记]javascript的数据类型