JavaScript Object.prototype.constructor Property

The conttructor property returns a reference to the object constructor function that has created the instance of an object. The value of the constructor is not a string containing the function’s name, but it is a reference to the function itself.

Syntax:

Object.constructor

Return Value: It is a reference to the object of constructor.

Example 1: Below example illustrates how to display the constructor of an object.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script type="text/javascript">
        function Gfg(name) {
            this.name = name
        }
        let GeeksforGeeks = new Gfg('Geeks');
        console.log(GeeksforGeeks.constructor);
    </script>
</body>
  
</html>

chevron_right


Output:



Example 2: Below example illustrates how to change the constructor of an object.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script type="text/javascript">
        function Types() { }
        let types = [
            new Array(),
            [],
            new Boolean(),
            false,
            new Date(),
            new Error(),
            new Function(),
            new RegExp(),
            /(?:)/
        ]
        let j = 0;
        while (j < types.length) {
            types[j].constructor = Types
            types[j] = [types[j].constructor,
            types[j] instanceof Types,
            types[j].toString()]
            ++j;
        }
        console.log(types.join('\n'));
    </script>
</body>
  
</html>

chevron_right


Output:

Browser supported:

  • Chrome
  • Firefox
  • Edge
  • Internet Explorer
  • Safari
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.