JavaScript資料資料型別判斷
阿新 • • 發佈:2021-01-09
- 資料型別
基本資料型別: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
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 |