数据类型的检测

//1、 typeof
var a = 12;
typeof a// ---> 'number'
typeof 'number' // ---> 'string'
typeof true // ----> 'boolean'
typeof 'true' // ---> 'string'
typeof null // ---> 'object'
typeof undefined // ---> 'undefined'
typeof ({}) // ---> 'object'
typeof ([]) // ---> 'object'

//2、constructor

var str = 'www';
console.log(str.constructor.toString());
console.log(a.constructor.toString());
console.log(({}).constructor.toString());
console.log(([]).constructor.toString());
// console.log(null.constructor);

//3、 instanceof 查看对象
var obj = {};
var ary = [];
console.log(obj instanceof Array); // false
console.log(ary instanceof Array); // true
console.log(obj instanceof Object);// true
console.log(ary instanceof Object);// true

//4、Object.prototype.toString.call(xxx);
console.log(Object.prototype.toString.call([]));
console.log(Object.prototype.toString.call({}));
console.log(Object.prototype.toString.call(/\d/));
console.log(Object.prototype.toString.call(12));
console.log(Object.prototype.toString.call('www'));
console.log(Object.prototype.toString.call(true));
posted @ 2018-08-29 13:49  —只小白  阅读(104)  评论(0编辑  收藏  举报