如何在 JavaScript 中检查一个值是否类似对象?


JavaScript 中,**类似对象**的值是指既不是原始值也不是未定义的值。类似对象的值是任何不是原始值的值,包括函数、数组和对象。在 JavaScript 中,有多种方法可以检查一个值是否类似对象。在本文中,我们将介绍三种在 JavaScript 中检查值是否类似对象的方法。

使用 typeof 运算符

**typeof** 运算符是 JavaScript 中一个内置运算符,用于检查值的类型。typeof 运算符返回一个字符串,表示值的类型。typeof 运算符可用于检查 JavaScript 中的值是否类似对象。

示例 1

Open Compiler
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result1"></div> <div id="result2"></div> <div id="result3"></div> <script> document.getElementById("result1").innerHTML ="typeof {}: " + typeof {}; document.getElementById("result2").innerHTML ="typeof []: " + typeof []; document.getElementById("result3").innerHTML ="typeof function(){}: " + typeof function(){}; </script> </body> </html>

以下是上述代码片段的详细解释

在 script 标签中的第一个语句中,我们检查空对象 {} 的类型。typeof 运算符对于空对象返回字符串 "object"。在第二个语句中,我们检查空数组 [] 的类型。typeof 运算符对于空数组返回字符串 "object"。在第三个语句中,我们检查匿名函数 function(){} 的类型。typeof 运算符对于匿名函数返回字符串 "function"。

Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.

使用 instanceof 运算符

instanceof 运算符是 JavaScript 中一个内置运算符,用于检查一个值是否是构造函数的实例。instanceof 运算符返回一个布尔值,如果该值是构造函数的实例,则为 true,否则为 false。instanceof 运算符可用于检查 JavaScript 中的值是否类似对象。

示例 2

Open Compiler
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result1"></div> <div id="result2"></div> <div id="result3"></div> <script> document.getElementById("result1").innerHTML = "Is {} an instance of Object: " + ({} instanceof Object); document.getElementById("result2").innerHTML = "Is [] an instance of Array: " +([] instanceof Array); document.getElementById("result3").innerHTML ="Is function(){} is an instance of Function: " + (function(){} instanceof Function); </script> </body> </html>

在 script 标签中,第一个语句检查空对象 {} 是否是 Object 构造函数的实例。instanceof 运算符对于空对象返回布尔值 true。第二个语句检查空数组 [] 是否是 Array 构造函数的实例。instanceof 运算符对于空数组返回布尔值 true。第三个语句检查匿名函数 function(){} 是否是 Function 构造函数的实例。instanceof 运算符对于匿名函数返回布尔值 true。

Object.prototype.toString() 方法

**Object.prototype.toString()** 方法是 JavaScript 中一个内置方法,用于将对象转换为字符串。Object.prototype.toString() 方法可用于检查 JavaScript 中的值是否类似对象。

示例 3

Open Compiler
<!doctype html> <html> <head> <title>Examples</title> </head> <body> <div id="result1"></div> <div id="result2"></div> <div id="result3"></div> <script> document.getElementById("result1").innerHTML = Object.prototype.toString.call({}) document.getElementById("result2").innerHTML = Object.prototype.toString.call([]) document.getElementById("result3").innerHTML = Object.prototype.toString.call(function(){}) </script> </body> </html>

在 script 标签中的第一个语句中,我们使用 Object.prototype.toString() 方法将空对象 {} 转换为字符串。Object.prototype.toString() 方法对于空对象返回字符串 "[object Object]"。

在 script 标签中的第二个语句中,我们使用 Object.prototype.toString() 方法将空数组 [] 转换为字符串。Object.prototype.toString() 方法对于空数组返回字符串 "[object Array]"。

在第三个语句中,我们使用 Object.prototype.toString() 方法将匿名函数 function(){} 转换为字符串。Object.prototype.toString() 方法对于匿名函数返回字符串 "[object Function]"。

结论

在本教程中,我们介绍了三种不同的方法来检查 JavaScript 中的值是否类似对象。我们介绍了 typeof 运算符、instanceof 运算符和 Object.prototype.toString() 方法。所有这些方法都可以用来检查 JavaScript 中的值是否类似对象。

更新于:2022年6月24日

浏览量 105

启动您的 职业生涯

完成课程获得认证

开始学习
广告