JavaScript - setPrototypeOf() method

revision:


Category : object

The Object.setPrototypeOf() static method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null..

Syntax :

        Object.setPrototypeOf(obj, prototype)
    

Parameters:

obj : required. The object which is to have its prototype set.

prototype : required. The object's new prototype (an object or null).

Examples:

        const obj = {};
        const parent = { foo: 'bar' };
        console.log(obj.foo);
        // Expected output: undefined
        Object.setPrototypeOf(obj, parent);
        console.log(obj.foo);
        // Expected output: "bar"
    

Practical examples

example: set a prototype of an object.

code:
                    <div>
                        <p id="proto-1"></p>
                        <p id="proto-2"></p>
                        <p id="proto-3"></p>
                        <p id="proto-4"></p>
                    </div>
                    <script>
                        let ninja1 = {
                            prop1() {
                                return 'Object.isExtensible()';
                            },
                            prop2() {
                                return 'JavaScript ';
                            }
                        }
                        let ninja2 = {
                            prop3() {
                                return 'Ninjasforninjas';
                            }
                        }
                        Object.setPrototypeOf(ninja2, ninja1);
                        
                        console.dir(ninja2);
                        console.log(ninja2.prop3());
                        console.log(ninja2.prop2());
                        console.log(ninja2.prop1());
            
                        document.getElementById("proto-1").innerHTML = (ninja2.prop3);
                        document.getElementById("proto-2").innerHTML = (ninja2.prop2);
                        document.getElementById("proto-3").innerHTML = (ninja2.prop1);
                    </script>
                

example: appending a chain to a prototype.

code:
                    <div>
                        <p id="proto-5"></p>
                        <p id="proto-6"></p>
                    </div>
                    <script>
                        function Mammal() {
                            this.isMammal = 'yes';
                        }
                        function MammalSpecies(sMammalSpecies) {
                            this.species = sMammalSpecies;
                        }
                        MammalSpecies.prototype = new Mammal();
                        MammalSpecies.prototype.constructor = MammalSpecies;
                        var oCat = new MammalSpecies('Felis');
                        console.log(oCat.isMammal); // 'yes'
                        document.getElementById("proto-5").innerHTML = "is cat a mammal ? : " + (oCat.isMammal);
                    
                    </script>
                

example: set the prototype of the specified object to another object or null.

code:
                    <div>
                        <p id="proto-7"></p>
                        <p id="proto-8"></p>
                        <p id="proto-9"></p>
                    </div>
                    <script>
                        // create the object to be set as the prototype
                        let Animal = {
                            makeSound() {
                                console.log(`${this.name}, ${this.sound}!`);
                                document.getElementById("proto-7").innerHTML = (`${this.name}, ${this.sound}!`);
                            },
                        };
                        // define the Dog object constructor
                        function Dog(name) {
                            this.name = name;
                            this.sound = "bark";
                            // set prototype of Dog object to Animal
                            Object.setPrototypeOf(this, Animal);
                        }
                        // create dog1 object using the Dog() constructor
                        dog1 = new Dog("Marcus");
                        // call the makeSound() method of Animal
                        dog1.makeSound();
                        // Output: Marcus, bark!
                    </script>