Ad Code

Responsive Advertisement

Inheritance | Prototype

In javascript everything is an object.


    <script>
        let obj1 = {};
        obj1.name = "obj1";
        obj1.run = function (){
            console.log("Obj1 can run !");
        };

        let obj2 = {
            colour : "red"
        };

        console.log(obj1.__proto__.__proto__);

        obj2.__proto__ = obj1;    // obj1 stored in obj2's prototype object
        console.log(obj2.name);
        obj2.run();
    </script>

In example, obj1 is stored in prototype object of obj2 so it can accessible by obj2 methods.


Post a Comment

0 Comments