Home  >  Article  >  Web Front-end  >  How to check if a value is object-like in JavaScript?

How to check if a value is object-like in JavaScript?

WBOY
WBOYforward
2023-08-24 16:13:02488browse

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

In JavaScript, Object-likeA value is a value that is not primitive and is not undefined. An object-like value is any value that is not a primitive, including functions, arrays, and objects. There are different ways in JavaScript to check if a value is an object. In this article, we will introduce 3 ways to check if a value is a class object in JavaScript.

Using the typeof operator

typeof The typeof operator is a built-in operator in JavaScript that is used to check the type of a value. The typeof operator returns a string that is the type of the value. The typeof operator can be used to check whether a value in JavaScript is similar to an object.

Example 1

<!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>

Here is a detailed explanation of the above code snippet:

In the first statement in the script tag, we are checking for an empty object {} type. this The typeof operator returns the string "object" of the empty object. In the second statement, We are checking the type of empty array []. typeof operator returns a string "object" means an empty array. In the third statement we are checking the type of an Anonymous function function(){}. The typeof operator returns the string "function" Anonymous function.

Use the instanceof operator:

The instanceof operator is a built-in operator in JavaScript that is used to check whether a value Is an instance of a constructor function. instanceof operator returns a boolean value value, true if the value is an instance of the constructor; value is not an instance of the constructor. You can use the instanceof operator Check if a value in JavaScript is object-like.

Example 2

<!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>

In the script tag, in the first statement, we check if the empty object {} is a Instance of object constructor. instanceof operator returns The Boolean value true for an empty object. In the second statement we check whether Empty array[] is an instance of the Array constructor. instance operator Returns Boolean true for an empty array. In the third statement we are checking If the anonymous function function(){} is an instance of the Function constructor. For anonymous functions, the instanceof operator returns the Boolean value true.

Object.prototype.toString() method:

Object.prototype.toString() Strong> method is a built-in method in JavaScript for converting objects to strings . The Object.prototype.toString() method can be used to check whether a value in JavaScript is object-like.

Example 3

<!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>

In the first statement within the script tag, we use The Object.prototype.toString() method converts an empty object {} to a string. this The Object.prototype.toString() method returns the string "[object Object]" Empty object.

In the second statement inside the script tag, we use The Object.prototype.toString() method converts an empty array[] to a string. this Object.prototype.toString() method returns empty string "[object Array]"

In the third statement, we use the Object.prototype.toString() method to Convert anonymous function function(){} to string. this The Object.prototype.toString() method returns the string "[object Function]" Anonymous function.

Conclusion

In this tutorial we looked at three different ways to check if a value is similar to an object JavaScript. We looked at the typeof operator, instanceof operator, and Object.prototype.toString() method. All these methods can be used to check whether a value is Similar to objects in JavaScript.

The above is the detailed content of How to check if a value is object-like in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete