Home  >  Article  >  Web Front-end  >  How to check if an object's constructor is a JavaScript object?

How to check if an object's constructor is a JavaScript object?

WBOY
WBOYforward
2023-09-15 08:25:05639browse

如何检查对象的构造函数是否是 JavaScript 对象?

In this article, we will check if the constructor of an object is a JavaScript object. The constructor property of any JavaScript variable returns a reference to the Object constructor that created the instance object. The value of this property is a reference to the function itself.

All objects have a constructor attribute, and objects created without a constructor will have a constructor attribute pointing to the base object constructor type.

To check if the constructor of the provided value is an object created by the object constructor, we need to compare the object's constructor property value with the corresponding object constructor reference. The constructor property returns a reference to the constructor that created the instance.

Syntax

The following is the function syntax to check whether the constructor of an object is Object

function check(obj) {
   return obj.constructor === Object ? true : false
}

Example

In the following program, we check six Whether the object's constructor is a JavaScript object.

<html>
<body>
   <h3 >Check if Constructor is Object</h3>
   <p> Click on the check button all test cases </p>
   <p>Test Case 1: {} </p>
   <p>Constructor is Object:
      <span id="testcase1"> </span> </p>
   <p>Test Case 2: new Number(3)</p>
   <p>Constructor is Object:
      <span id="testcase2"> </span> </p>
   <p>Test Case 3: new Object </p>
   <p>Constructor is Object:
      <span id="testcase3"> </span> </p>
   <p>Test Case 4: new Object() </p>
   <p>Constructor is Object:
      <span id="testcase4"> </span> </p>
   <p> Test Case 5: [] </p>
   <p>Constructor is Object:
      <span id="testcase5"> </span> </p>
   <p>Test Case 6: "Object Constructor" </p>
   <p>Constructor is Object:
      <span id="testcase6"> </span> </p>
   <button onclick="runTestCases()"> Check </button>
   <script>

      // This function will check if created by Object constructor
      function check(obj) {
         return obj.constructor === Object ? true : false
      }
      function runTestCases() {
         document.getElementById("testcase1").textContent =
            check({});
         document.getElementById("testcase2").textContent =
            check(new Number(3));

         document.getElementById("testcase3").textContent =
            check(new Object);

         document.getElementById("testcase4").textContent =
            check(new Object());

         document.getElementById("testcase5").textContent =
            check([]);

         document.getElementById("testcase6").textContent =
            check("Object Conctructor");
      }
   </script>
</body>
</html>

When the "Check" button is clicked, all the test cases will run and show the output as true or false. As we can see in the above code, if the object is created by the object constructor, the result will be reflected as true, otherwise it will be displayed as false. In the above code, test cases 1, 3, and 4 result in true because they are all created using the object constructor. Here, the object constructor property returns a value equal to the object in cases 1, 3, and 4.

Example (Find the constructor of an object)

In the following program, we find the constructors of four different objects created using different methods. We use the Object.constructor property to find the object's constructor.

<html>
<body>
   <h3> Find the Constructor of Objects</h3>
   <p id="demo"></p>
   <script>
      function Student(first, last, course) {
         this.firstName = first;
         this.lastName = last;
         this.course = course;
      }
      const stud1 = new Student("John", "Doe", "M.Tech");
      const stud2 = new Object();
      const stud3 = new Object;
      var stud4 = {
         firstName: "John",
         lastName: "Doe",
         course: "M.Tech"
      }
      document.getElementById("demo").innerHTML +=`Constructor of stud1: ${stud1.constructor} <br>`;
      document.getElementById("demo").innerHTML +=`<br>Constructor of stud2: ${stud2.constructor} <br>`;
      document.getElementById("demo").innerHTML +=`<br>Constructor of stud3: ${stud3.constructor} <br>`;
      document.getElementById("demo").innerHTML +=`<br>Constructor of stud4: ${stud4.constructor} <br>`;
   </script>
</body>
</html>

The above is the detailed content of How to check if an object's constructor is a JavaScript object?. 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