- 数据类型
基本数据类型:string、number、boolean、null、undefined、symbol
引用数据类型:object、array、function - typeof
typeof操作符返回一个字符串,表示未经计算的操作数的类型
console.log(typeof 42);
// expected output: "number"
console.log(typeof 'blubber');
// expected output: "string"
console.log(typeof true);
// expected output: "boolean"
console.log(typeof undeclaredVariable);
// expected output: "undefined"
console.log(typeof null);
// expected output: "object"
var a = Symbol(3)
console.log(typeof a);
// expected output: "symbol"
var arr = [1,2,3]
console.log(typeof arr);
// expected output: "object"
var obj = {a:1}
console.log(typeof obj);
// expected output: "object"
var func = function () {
console.log(1)
}
console.log(typeof func);
// expected output: "function"
typeof 可以正确检测出number、string、boolean、undefined、symbol、function,而null、array、object检测结果均为object,无法准确判断
3. instanceof
instanceof运算符用来检测构造函数的prototype属性是否出现在某个实例对象的原型链上
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car);
// expected output: true
console.log(auto instanceof Object);
// expected output: true
var num = 2
console.log(num instanceof Number);
// expected output: false
var bool = false
console.log(bool instanceof Boolean);
// expected output: false
var str = 'string1'
console.log(str instanceof String);
// expected output: false
var temp = Symbol(2)
console.log(temp instanceof Symbol);
// expected output: false
console.log(null instanceof Object);
// expected output: false
console.log(undefined instanceof Object);
// expected output: false
var arr = [1,2,3]
console.log(arr instanceof Array);
// expected output: true
var func = function() {console.log('123')}
console.log(func instanceof Function);
// expected output: true
var obj = {a:1}
console.log(obj instanceof Object);
// expected output: true
instanceof不能区别undefined和null,基本类型不用new声明的也检测不出来
4. constructor
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto.constructor === Car);
// expected output: true
var num = 2
console.log(num.constructor === Number);
// expected output: true
var bool = false
console.log(bool.constructor === Boolean);
// expected output: true
var str = 'string1'
console.log(str.constructor === String);
// expected output: true
var temp = Symbol(2)
console.log(temp.constructor === Symbol);
// expected output: true
//console.log(null.constructor === Object);
// Error: Cannot read property 'constructor' of null
//console.log(undefined.constructor === Object);
// Error: Cannot read property 'constructor' of undefined
var arr = [1,2,3]
console.log(arr.constructor === Array);
// expected output: true
var func = function() {console.log('123')}
console.log(func.constructor === Function);
// expected output: true
var obj = {a:1}
console.log(obj.constructor === Object);
// expected output: true
constructor无法判断null和undefined
5. toString
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(Object.prototype.toString.call(auto));
// expected output: [object Object]
var num = 2
console.log(Object.prototype.toString.call(num));
// expected output: [object Number]
var bool = false
console.log(Object.prototype.toString.call(bool));
// expected output: [object Boolean]
var str = 'string1'
console.log(Object.prototype.toString.call(str));
// expected output: [object String]
var temp = Symbol(2)
console.log(Object.prototype.toString.call(temp));
// expected output: [object Symbol]
console.log(Object.prototype.toString.call(null));
// expected output: [object Null]
console.log(Object.prototype.toString.call(undefined));
// expected output: [object Undefined]
var arr = [1,2,3]
console.log(Object.prototype.toString.call(arr));
// expected output: [object Array]
var func = function() {console.log('123')}
console.log(Object.prototype.toString.call(func));
// expected output: [object Function]
var obj = {a:1}
console.log(Object.prototype.toString.call(obj));
// expected output: [object Object]
在任何值上调用object原生的toString()方法,都会返回一个[Object NativeConstructorName]格式的字符串
6. 常用判断
说明 | 方法 |
---|---|
是否是数字 | !isNaN(x) |
是否是数组 | Array.isArray(x) |
是否是对象 | Object.prototype.toString.call(xx) == ‘[Object Object]’ |
是否是空对象 | JSON.stringify(xx) === ‘{}’ |
是否是null或undefined | x == null |
是否是null | x === null |
是否是undefined | x === undefined |