How to check if the constructor of an object is a JavaScript Object?


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

All the objects have the constructor property and the objects created without the constructor function will have a constructor property that points to the Fundamental Object constructor type for that object.

To check whether the constructor of provided value is an Object created by the object constructor function or not we need to compare the value of the object’s constructor property value with the corresponding Object constructor function reference. The constructor property returns a reference to the constructor function that created the instance.

Syntax

Following is the syntax of the function to check if the constructor of an object is Object

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

Example

In the program below, we check six objects if their constructor is 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>

On clicking the “check” button all the test cases will run and show the output as true or false. As we can see in the above code it will reflect the result as true if the object is created by the object constructor otherwise it will show the result as false. In the above code, we are getting true for test cases 1,3, and 4 because all of them are created using an object constructor. Here the object constructor property is returning value as equal to the object for 1st, 3rd and 4th cases.

Example (Find Constructor of an Object)

In the program below, we find the constructor of four different objects created using different methods. We apply Object.constructor property to find the constructor of an object.

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

Updated on: 21-Jul-2022

827 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements