07 JS 数据类型
typeof 操作符¶
typeof操作符是最简单的判断数据类型的方法之一。它返回一个表示数据类型的字符串。
let stringValue = "Hello, World!";
let numberValue = 100;
let booleanValue = true;
let objectValue = { key: "value" };
let arrayValue = [1, 2, 3];
let functionValue = function() {};
console.log(typeof stringValue); // "string"
console.log(typeof numberValue); // "number"
console.log(typeof booleanValue); // "boolean"
console.log(typeof objectValue); // "object"
console.log(typeof arrayValue); // "object" (数组在JavaScript中是对象类型)
console.log(typeof functionValue); // "function"
Array.isArray() 方法¶
对于数组类型,可以使用Array.isArray()方法来判断一个变量是否是数组。
Object.prototype.toString.call() 方法¶
Object.prototype.toString.call()方法可以返回一个对象的完整类型信息。通过调用.call()方法并传递需要检查的值,可以获得其类型。
let stringValue = "Hello, World!";
let numberValue = 100;
let booleanValue = true;
let objectValue = { key: "value" };
let arrayValue = [1, 2, 3];
let functionValue = function() {};
console.log(Object.prototype.toString.call(stringValue)); // "[object String]"
console.log(Object.prototype.toString.call(numberValue)); // "[object Number]"
console.log(Object.prototype.toString.call(booleanValue)); // "[object Boolean]"
console.log(Object.prototype.toString.call(objectValue)); // "[object Object]"
console.log(Object.prototype.toString.call(arrayValue)); // "[object Array]"
console.log(Object.prototype.toString.call(functionValue)); // "[object Function]"
instanceof 操作符¶
instanceof操作符用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上。
constructor 属性¶
每个对象都有一个constructor属性,指向它的构造函数。通过这个属性,你可以判断对象的类型。
let stringValue = "Hello, World!";
let numberValue = 100;
let booleanValue = true;
let objectValue = { key: "value" };
let arrayValue = [1, 2, 3];
let functionValue = function() {};
console.log(stringValue.constructor === String); // true
console.log(numberValue.constructor === Number); // true
console.log(booleanValue.constructor === Boolean); // true
console.log(objectValue.constructor === Object); // true
console.log(arrayValue.constructor === Array); // true
console.log(functionValue.constructor === Function); // true
注意:¶
typeof无法正确判断数组和对象,因为它们都是返回"object"。Object.prototype.toString.call()方法可以提供最精确的类型信息,但需要处理返回值字符串。instanceof操作符在判断对象类型时非常有效,但无法判断基本数据类型。constructor属性可以被修改,所以在某些情况下可能不可靠。