7. 如何判断对象的类型?
约 783 字大约 3 分钟
2025-03-12
一、常见方式
常见的方式主要是一下几种,我们来判断一下优缺点:
1. typeof
用于确定变量的数据类型,它返回一个表示数据类型的字符串,而不是实际的数据类型。
功能: 适用于
基本数据类型
和function类型
的判断。可以区分缺陷: 无法判断通过出
原始数据类型(如字符串、数值、布尔值)
和函数类型
以外的 其他数据类型(返回object
),也无法判断通过构造函数
、类
封装的 自定义对象 。
typeof "hello"; // 返回 "string"
typeof 42; // 返回 "number"
typeof true; // 返回 "boolean"
typeof function () {}; // 返回 "function"
typeof undefined; // 返回 "undefined"
typeof null; // 返回 "object"
2. instanceof
用于检查对象是否属于某个类或构造函数的实例。它会通过检查对象的原型链来确定对象是否是指定类的实例。返回一个布尔值的结果。
功能: instanceof 适用于 判断对象的具体类型,它可以判断某个对象是否属于 某个特定的构造函数或类的实例,因此 instanceof 可以判断通过
构造函数
、类
封装的 自定义对象 。缺陷: instanceof 无法判断 原始数据类型。
例如:
var arr = [1, 2, 3];
arr instanceof Array; // 返回 true
var obj = {};
obj instanceof Object; // 返回 true
var str = "hello";
str instanceof String; // 返回 false
// 构造函数封装的对象
function Person(name, age) {
this.name = name;
this.age = age;
this.greet = function () {
console.log(
"Hello, " + this.name + "! You are " + this.age + " years old."
);
};
}
const person1 = new Person("John", 30);
person1.greet(); // 返回 Hello, John! You are 30 years old.
console.log(person1 instanceof Person); // 返回 true
3. Object.prototype.toString.call();
Object.prototype.toString.call(target)
会返回target
对应的 "[object class_name]"
。我们通过截取返回值,拿到对应类型结果字符串 "class_name"
功能: 可以判断基础数据类型和封装数据类型
缺陷: 无法判断自定义对象(通过构造函数,class 封装),因为返回结果为
'[object Object]'
。
console.log(Object.prototype.toString.call([])); // [object Array]
console.log(Object.prototype.toString.call({})); // [object Object]
console.log(Object.prototype.toString.call(123)); // [object Number]
console.log(Object.prototype.toString.call("hello")); // [object String]
console.log(Object.prototype.toString.call(true)); // [object Boolean]
console.log(Object.prototype.toString.call(null)); // [object Null]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(Object.prototype.toString.call(new Date())); // [object Date]
console.log(Object.prototype.toString.call(function () {})); // [object Function]
优化
在此,我们将结果继续优化为Object.prototype.toString.call(value).slice(8, -1).toLowerCase()
;
// 优化一下
function getType(value) {
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}
console.log(getType([])); // "array"
console.log(getType({})); // "object"
console.log(getType(123)); // "number"
console.log(getType("hello")); // "string"
console.log(getType(null)); // "null"
console.log(getType(undefined)); // "undefined"
console.log(getType(new Date())); // "date"
console.log(getType(function () {})); // "function"
二、总结
综上,我们可以利用Object.prototype.toString.call(value)
来完成对对象的判断,但是比较特殊的是某些自定义封装的对象,会返回Object
。我们需要利用instantof
进行进一步判断。
// 获取对象的类型
function getType(value) {
if (value === null) return "null";
if (value === undefined) return "undefined";
const typeString = Object.prototype.toString.call(value);
const basicType = typeString.slice(8, -1).toLowerCase();
if (basicType === "object") {
const constructorName = value.constructor.name;
if (constructorName !== "Object") {
return constructorName.toLowerCase();
}
}
return basicType;
}
// 判断原型是否相等
function isInstanceOf(value, constructor) {
return value instanceof constructor;
}
// 使用示例
class Person {}
class Student extends Person {}
const student = new Student();
console.log(getType(student)); // "student"
console.log(isInstanceOf(student, Person)); // true
更新日志
e7112
-1于