Object Oriented Programming
Object
Objects are tangible things people can observe and interact with. These qualities, or properties, define what makes up an object. Note that similar objects share the same properties, but may have different values for those properties.
    let object_1 = {
        name: "Ashish Ranjan",
        date: "April 26, 2022"
    };
    console.log(object_1); // {name: 'Ashish Ranjan', date: 'April 26, 2022'}
access objects
    let object_1 = {
        name: "Ashish Ranjan",
        date: "April 26, 2022"
    };
    console.log(`name: ${object_1.name} and date: ${object_1.date}`); // name: Ashish Ranjan and date: April 26, 2022
method of an object
    let object_1 = {
        name: "Ashish Ranjan",
        date: "April 26, 2022",
        get_data: function() { return `function inside object: name: ${object_1.name} and date: ${object_1.date}`; },
    };
    console.log(`object_1.get_data():: ${object_1.get_data()}`); // object_1.get_data():: function inside object: name: Ashish Ranjan and date: April 26, 2022
this keyword
    let object_1 = {
        first_name: "Ashish",
        last_name: "Ranjan",
        get_data: function() { return `first_name: ${this.first_name} and last_name: ${this.last_name}`; },
    };
    console.log(`object_1 returns from function: ${object_1.get_data}`); // object_1 returns from function: function() { return `first_name: ${this.first_name} and last_name: ${this.last_name}`; }
constructors
Constructors are functions that create new objects. They define properties and behaviors that will belong to the new object. They are a blueprint for the creation of new objects.
    function constructor_function() {
        this.first_name = "Ashish";
        this.last_name = "Ranjan";
    }
create objects with constructors
    function constructor_function() {
        this.name = "Ashish Ranjan";
        this.date = "April 26, 2022";
        this.get_data = ()=>{return `this.name: ${this.name} and thie.date: ${this.date}`};
    }
    let test_variable = new constructor_function();
    test_variable.name = "new name";
    console.log(`object values are: ${test_variable.get_data()}`); // object values are: this.name: new name and thie.date: April 26, 2022
constructors receive arguments
    function constructor_arguments(first_name,last_name) {
        this.first_name = first_name;
        this.last_name = last_name;
    }
    let variable_1 = new constructor_arguments("Ashish","Ranjan");
    let variable_2 = new constructor_arguments("first_name","second_name");
    console.log(`variable 1 first name: ${variable_1.first_name} second name: ${variable_1.last_name}`); // variable 1 first name: Ashish second name: Ranjan
    console.log(`variable 2 first name: ${variable_2.first_name} second name: ${variable_2.last_name}`); // variable 2 first name: first_name second name: second_name
instanceof operator
    // let object_1 = (full_name,date) => {
        //     this.full_name = full_name;
        //     this.date = date;
        // };
        // above object_1 is not a constructor
        let object_1 = function (full_name,date) {
            this.full_name = full_name;
            this.date = date;
        };
        let variable_1 = new object_1("Ashish Ranjan", "April 06, 2022");
        let object_2 = function (first_name,last_name) {
            this.first_name = first_name;
            this.last_name = last_name;
        };
        let variable_2 = new object_2("Ashish","Ranjan");
        console.log(`variable_1 instance of object_1:: ${variable_1 instanceof object_1}`); // variable_1 instance of object_1:: true
        console.log(`variable_2 instance of object_1:: ${variable_2 instanceof object_1}`); // variable_2 instance of object_1:: false
        console.log(`variable_1 instance of object_2:: ${variable_1 instanceof object_2}`); // variable_1 instance of object_2:: false
        console.log(`variable_2 instance of object_2:: ${variable_2 instanceof object_2}`); // variable_2 instance of object_2:: true
own property
    let object_1 = function (first_name,last_name) {
        this.first_name = first_name;
        this.last_name = last_name;
    };
    let variable_1 = new object_1("ashish1","ranjan1");
    let variable_2 = new object_1("ashish2","ranjan2");

    let variable1_properties_array = [];
    for (let property in variable_1) {
        if (variable_1.hasOwnProperty(property)) {
            variable1_properties_array.push(property);
        }
    }
    console.log(`variable1_properties_array:: "${variable1_properties_array}"`); // variable1_properties_array:: "first_name,last_name"
prototype properties to reducew duplicate code
All JavaScript objects inherit properties and methods from a prototype. The prototype is as a "recipe" for creating objects.
    let object_1 = function(first_name,last_name) {
        this.first_name = first_name;
        this.last_name = last_name;
    };
    let variable_1 = new object_1("Ashish1","Ranjan1");
    let variable_2 = new object_1("Ashish2","Ranjan2");
    object_1.prototype.place = "Bengaluru";
    console.log(`variable_1 values = ${JSON.stringify(variable_1)} and prototype place: ${variable_1.place}`); // variable_1 values = {"first_name":"Ashish1","last_name":"Ranjan1"} and prototype place: Bengaluru
    console.log(`variable_2 values = ${JSON.stringify(variable_2)} and prototype place: ${variable_2.place}`); // variable_2 values = {"first_name":"Ashish2","last_name":"Ranjan2"} and prototype place: Bengaluru
iterate over object properties
    let object_1 = function(first_name,last_name) {
        this.first_name = first_name; // own property
        this.last_name = last_name; // own property
    };
    object_1.prototype.place = "bengaluru"; // prototype property
    object_1.prototype.country = "india"; // prototype property
    let variable_1 = new object_1("ashish","ranjan");

    let own_property = [];
    let prototype_property = [];
    for (let property in variable_1) {
        if (variable_1.hasOwnProperty(property)) {
            own_property.push(property);
        } else {
            prototype_property.push(property);
        }
    }
    console.log(`own properties:: ${JSON.stringify(own_property)}`); // own properties:: ["first_name","last_name"]
    console.log(`prototype properties:: ${JSON.stringify(prototype_property)}`); // prototype properties:: ["place","country"]
constructor property
There is a special constructor property located on the object instances. The constructor property is a reference to the constructor function that created the instance.
    let object_1 = function(first_name,last_name){
        this.first_name = first_name;
        this.last_name = last_name;
    };
    let variable_1 = new object_1("ashish1","ranjan1");
    let variable_2 = new object_1("ashish2","ranjan2");

    console.log(variable_1.constructor === object_1); // true
    console.log(variable_2.constructor === object_1); // true
add prototype from new object
    let object_1 = function (first_name, last_name) {

        // console.log({first_name});
        // console.log({last_name});
        this.last_name = last_name;
        this.first_name = first_name;
    };

    let variable_1 = new object_1("ashish1", "ranjan1");

    console.log(variable_1, 'variable_1')
    object_1.prototype.place = "bengaluru";
    object_1.prototype.country = "india";
    object_1.prototype.datentime = () => { console.log("today is April 07, 2022"); };
    object_1.prototype.phonenumber = () => { console.log("1234567890"); };

    // console.log(object_1, "object_1")
    let own_property = [];
    let prototype_property = [];
    for (let property in variable_1) {
        console.log(property, 'property')
        if (variable_1.hasOwnProperty(property)) {
            own_property.push(property);
        } else {
            prototype_property.push(property);
        }
    }
    console.log(`own properties:: ${JSON.stringify(own_property)}`); // own properties:: ["last_name","first_name"]
    console.log(`prototype properties:: ${JSON.stringify(prototype_property)}`); // prototype properties:: ["place","country","datentime","phonenumber"]

    object_1.prototype = {
        property_3: "value 3",
        property_4: () => console.log(this.first_name),
        property_5: () => console.log("a2rp: an Ashish Ranjan presentation"),
    };
    let own_property_2 = [];
    let prototype_property_2 = [];
    let variable_2 = new object_1("ashish1", "ranjan1");
    for (let property in variable_2) {
        if (variable_2.hasOwnProperty(property)) {
            own_property_2.push(property);
        } else {
            prototype_property_2.push(property);
        }
    }
    console.log(`own properties:: ${JSON.stringify(own_property_2)}`); // own properties:: ["last_name","first_name"]
    console.log(`prototype properties:: ${JSON.stringify(prototype_property_2)}`); // prototype properties:: ["property_3","property_4","property_5"]
set constructor property when changing the prototype
There is one crucial side effect of manually setting the prototype to a new object. It erases the constructor property! This property can be used to check which constructor function created the instance, but since the property has been overwritten, it now gives false results:
    let object_1 = function(first_name,last_name) {
        this.first_name = first_name, // own property
        this.last_name = last_name // own property
    };
    let variable_1 = new object_1("ashish1","ranjan1");
    console.log(`variable_1.constructor === object_1:: ${variable_1.constructor === object_1}`); // variable_1.constructor === object_1:: true
    console.log(`variable_1.constructor === Object:: ${variable_1.constructor === Object}`); // variable_1.constructor === Object:: false
    console.log(`variable_1 instanceof object_1:: ${variable_1 instanceof object_1}`); // variable_1 instanceof object_1:: true

    object_1.prototype.place = "bengaluru"; // prototype property

    // prototype property:: all properties in one
    object_1.prototype = {
        constructor: object_1,
        property_1: "value 1",
        property_2: () => console.log("value 2"),
        property_3: () => console.log("value 3"),
    };

    let variable_2 = new object_1("ashish2","ranjan2");

    let own_property_array = [];
    let prototype_property_array = [];
    for (let property in variable_2) {
        if (variable_2.hasOwnProperty(property)) {
            own_property_array.push(property);
        } else {
            prototype_property_array.push(property);
        }
    }
    console.log(`own properties:: ${JSON.stringify(own_property_array)}`); // own properties:: ["first_name","last_name"]
    console.log(`prototype properties:: ${JSON.stringify(prototype_property_array)}`); // prototype properties:: ["constructor","property_1","property_2","property_3"]
    console.log(`the constructor in protype object:: ${variable_2.constructor}`);
    // without constructor in prototype:: the constructor in protype object:: function Object() { [native code] }
    // with constructor in rpototype:: the constructor in protype object:: function(first_name,last_name) {
    //     this.first_name = first_name, // own property
    //     this.last_name = last_name // own property
    // }
objects prototype source
    let object_1 = function (first_name) {
        this.first_name =  first_name;
    };
    let variable_1 = new object_1("ashish");
    console.log(`object_1.prototype.isPrototypeOf(variable_1): ${object_1.prototype.isPrototypeOf(variable_1)}`); // object_1.prototype.isPrototypeOf(variable_1): true
prototype chain
All objects in JavaScript (with a few exceptions) have a prototype. Also, an object's prototype itself is an object. Because a prototype is an object, a prototype can have its own prototype! Object is a supertype for all objects in JavaScript. Therefore, any object can use the hasOwnProperty method.
    let object_1 = function(first_name,last_name) {
        this.first_name = first_name;
        this.last_name = last_name;
    };
    let variable_1 = new object_1("ashish1","ranjan1");
    console.log(`variable_1.hasOwnProperty("first_name"):: ${variable_1.hasOwnProperty("first_name")}`); // variable_1.hasOwnProperty("first_name"):: true
    console.log(`typeof object_1.prototype:: ${typeof object_1.prototype}`); // typeof object_1.prototype:: object
    console.log(`Object.prototype.isPrototypeOf(Bird.prototype:: ${Object.prototype.isPrototypeOf(object_1.prototype)})`); // Object.prototype.isPrototypeOf(Bird.prototype:: true)
DRY (Dont Repeat Yourself)
    function Cat(name) {
        this.name = name;
    }

    Cat.prototype = {
        constructor: Cat,
    };

    function Bear(name) {
        this.name = name;
    }

    Bear.prototype = {
        constructor: Bear,
    };

    function Animal() { }

    Animal.prototype = {
        constructor: Animal,
        eat: function() {
            console.log("nom nom nom");
        }
    };
Object.create
    function Animal() { }
    Animal.prototype = {
        constructor: Animal,
        eat: function() {
            // console.log("nom nom nom");
            return "nom nom nom";
        }
    };

    let duck = Object.create(Animal.prototype);
    console.log(`duck eat:: ${duck.eat()}`); // duck eat:: nom nom nom
    let beagle = Object.create(Animal.prototype);
    console.log(`beagle eat:: ${beagle.eat()}`); // beagle eat:: nom nom nom
Child's Prototype to an Instance of the Parent
    Animal.prototype = {
        constructor: Animal,
        eat: function() {
            console.log("nom nom nom");
        }
    };
    function Dog() { }
    
    let beagle = new Dog();
    let animal_instance = new Animal();
    Dog.prototype = Object.create(Animal.prototype);
    console.log(`${(new Dog).eat()}`);
Reset an Inherited Constructor Property
    function Animal() { }
    function Bird() { }
    function Dog() { }

    Bird.prototype = Object.create(Animal.prototype);
    Dog.prototype = Object.create(Animal.prototype);

    // Only change code below this line
    Bird.prototype.constructor = Bird;
    Dog.prototype.constructor = Dog;

    let duck = new Bird();
    let beagle = new Dog();
adding methods after inheritance
set the child prototype to instance of parent
    function Animal() {}
    Animal.prototype = {
        sound_1: "bark",
        sound_2: "speak",
    };

    function Bird() {}
    Bird.prototype = Object.create(Animal.prototype);

    let bird_variable = new Bird();
    let own_property_array = [];
    let prototype_property_array = [];
    for (let property in bird_variable) {
        if (bird_variable.hasOwnProperty(property)) {
            own_property.push(property);
        } else {
            prototype_property_array.push(property);
        }
    }
    console.log(`own_property ${own_property_array} prototype_property ${prototype_property_array}`); // own_property  prototype_property sound_1,sound_2
reset an inherited constructor property
When an object inherits its prototype from another object, it also inherits the supertype's constructor property.
    function Animal() {}
    Animal.prototype.make_noise = "bark";

    function Bird() {}
    Bird.prototype = Object.create(Animal.prototype);
    let duck = new Bird();
    console.log(duck.constructor); // ƒ Animal() {}

    Bird.prototype.constructor = Bird;
    console.log(duck.constructor); // ƒ Bird() {}
add methods after inheritance
    function Animal() {}
    Animal.prototype.eat = function() {
        console.log("eaten/ate");
    }
    function Bird() {}
    Bird.prototype = Object.create(Animal.prototype);
    let bird_object_1 = new Bird();
    console.log(`bird_object_1.constructor:: ${bird_object_1.constructor}`); // bird_object_1.constructor:: function Animal() {}

    Bird.prototype.constructor = Bird;
    let bird_object_2 = new Bird();
    console.log(`bird_object_2.constructor:: ${bird_object_2.constructor}`); // bird_object_2.constructor:: function Bird() {}

    Bird.prototype.fly = function() {
        console.log("m flying");
    };
    let object_3 = new Bird();
    object_3.eat(); // eaten/ate
    object_3.fly() // m flying
Override Inherited Methods
An object can inherit its behavior (methods) from another object by referencing its prototype object:
ChildObject.prototype = Object.create(ParentObject.prototype);
The ChildObject received its own methods by chaining them onto its prototype:
ChildObject.prototype.methodName = function() {...};
    function Animal() {}
    Animal.prototype.eat = function() { console.log("eat sound"); };

    function Bird() {}
    Bird.prototype = Object.create(Animal.prototype);
    let bird_object_1 = new Bird();
    bird_object_1.eat(); // eat sound

    Bird.prototype.eat = function() { console.log("sound of bird eating"); };
    bird_object_1.eat(); // sound of bird eating


<style>
 body {
 font-family: Consolas, Monaco, "Courier New", monospace;
 font-size: 11px;
 }

 pre {
 white-space: pre-wrap;
 /* Since CSS 2.1 */
 white-space: -moz-pre-wrap;
 /* Mozilla, since 1999 */
 white-space: -pre-wrap;
 /* Opera 4-6 */
 white-space: -o-pre-wrap;
 /* Opera 7 */
 word-wrap: break-word;
 /* Internet Explorer 5.5+ */
 background-color: #eee;
 padding: 15px;
 border-radius: 5px;
 }
</style>

<script type="text/javascript" src="jQuery_v3.6.0.js"></script>
<div style="font-family: Consolas, monospace; font-size: 11px;">

 <h1>Object Oriented Programming</h1>
 <hr />
 <hr />

 <h3>Object</h3>
 Objects are tangible things people can observe and interact with.
 These qualities, or properties, define what makes up an object.
 Note that similar objects share the same properties, but may have different values for those properties.

 <script type="text/javascript">
 $(document).ready(function () {
 console.log("a2rp: object oriented programming");
 let object_1 = {
 name: "Ashish Ranjan",
 date: "April 26, 2022"
 };
 console.log(object_1); // {name: 'Ashish Ranjan', date: 'April 26, 2022'}
 });
 </script>
 <pre>
 let object_1 = {
 name: "Ashish Ranjan",
 date: "April 26, 2022"
 };
 console.log(object_1); // {name: 'Ashish Ranjan', date: 'April 26, 2022'}
</pre>

 <hr />
 <hr />
 <h3>access objects</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(document).ready(function () {
 console.log("access object properties");
 let object_1 = {
 name: "Ashish Ranjan",
 date: "April 26, 2022"
 };
 console.log(`name: ${object_1.name} and date: ${object_1.date}`); // name: Ashish Ranjan and date: April 26, 2022
 });
 </script>
 <pre>
 let object_1 = {
 name: "Ashish Ranjan",
 date: "April 26, 2022"
 };
 console.log(`name: ${object_1.name} and date: ${object_1.date}`); // name: Ashish Ranjan and date: April 26, 2022
</pre>

 <hr />
 <hr />
 <h3>method of an object</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(document).ready(function () {
 let object_1 = {
 name: "Ashish Ranjan",
 date: "April 26, 2022",
 get_data: function () { return `function inside object: name: ${object_1.name} and date: ${object_1.date}`; },
 };
 console.log(`object_1.get_data():: ${object_1.get_data()}`); // object_1.get_data():: function inside object: name: Ashish Ranjan and date: April 26, 2022
 });
 </script>
 <pre>
 let object_1 = {
 name: "Ashish Ranjan",
 date: "April 26, 2022",
 get_data: function() { return `function inside object: name: ${object_1.name} and date: ${object_1.date}`; },
 };
 console.log(`object_1.get_data():: ${object_1.get_data()}`); // object_1.get_data():: function inside object: name: Ashish Ranjan and date: April 26, 2022
</pre>

 <hr />
 <hr />
 <h3>this keyword</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(document).ready(function () {
 let object_1 = {
 first_name: "Ashish",
 last_name: "Ranjan",
 get_data: function () { return `first_name: ${this.first_name} and last_name: ${this.last_name}`; },
 };
 console.log(`object_1 returns from function: ${object_1.get_data}`); // object_1 returns from function: function() { return `first_name: ${this.first_name} and last_name: ${this.last_name}`; }
 });
 </script>
 <pre>
 let object_1 = {
 first_name: "Ashish",
 last_name: "Ranjan",
 get_data: function() { return `first_name: ${this.first_name} and last_name: ${this.last_name}`; },
 };
 console.log(`object_1 returns from function: ${object_1.get_data}`); // object_1 returns from function: function() { return `first_name: ${this.first_name} and last_name: ${this.last_name}`; }
</pre>

 <hr />
 <hr />
 <h3>constructors</h3>
 <hr />
 <hr />
 Constructors are functions that create new objects.
 They define properties and behaviors that will belong to the new object.
 They are a blueprint for the creation of new objects.
 <script>
 $(() => {
 function constructor_function() {
 this.first_name = "Ashish";
 this.last_name = "Ranjan";
 }
 });
 </script>
 <pre>
 function constructor_function() {
 this.first_name = "Ashish";
 this.last_name = "Ranjan";
 }
</pre>

 <hr />
 <hr />
 <h3>create objects with constructors</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(() => {
 console.log("create objects with constructors");

 function constructor_function() {
 this.name = "Ashish Ranjan";
 this.date = "April 26, 2022";
 this.get_data = () => { return `this.name: ${this.name} and thie.date: ${this.date}` };
 }
 let test_variable = new constructor_function();
 test_variable.name = "new name";
 console.log(`object values are: ${test_variable.get_data()}`); // object values are: this.name: new name and thie.date: April 26, 2022
 });
 </script>
 <pre>
 function constructor_function() {
 this.name = "Ashish Ranjan";
 this.date = "April 26, 2022";
 this.get_data = ()=>{return `this.name: ${this.name} and thie.date: ${this.date}`};
 }
 let test_variable = new constructor_function();
 test_variable.name = "new name";
 console.log(`object values are: ${test_variable.get_data()}`); // object values are: this.name: new name and thie.date: April 26, 2022
</pre>

 <hr />
 <hr />
 <h3>constructors receive arguments</h3>
 <hr />
 <hr />

 <script type="text/javascript">
 $(() => {
 function constructor_arguments(first_name, last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 }
 let variable_1 = new constructor_arguments("Ashish", "Ranjan");
 let variable_2 = new constructor_arguments("first_name", "second_name");
 console.log(`variable 1 first name: ${variable_1.first_name} second name: ${variable_1.last_name}`); // variable 1 first name: Ashish second name: Ranjan
 console.log(`variable 2 first name: ${variable_2.first_name} second name: ${variable_2.last_name}`); // variable 2 first name: first_name second name: second_name
 });
 </script>
 <pre>
 function constructor_arguments(first_name,last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 }
 let variable_1 = new constructor_arguments("Ashish","Ranjan");
 let variable_2 = new constructor_arguments("first_name","second_name");
 console.log(`variable 1 first name: ${variable_1.first_name} second name: ${variable_1.last_name}`); // variable 1 first name: Ashish second name: Ranjan
 console.log(`variable 2 first name: ${variable_2.first_name} second name: ${variable_2.last_name}`); // variable 2 first name: first_name second name: second_name
</pre>

 <hr />
 <hr />
 <h3>instanceof operator</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(() => {
 // let object_1 = (full_name,date) => {
 // this.full_name = full_name;
 // this.date = date;
 // };
 // above object_1 is not a constructor
 let object_1 = function (full_name, date) {
 this.full_name = full_name;
 this.date = date;
 };
 let variable_1 = new object_1("Ashish Ranjan", "April 06, 2022");
 let object_2 = function (first_name, last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_2 = new object_2("Ashish", "Ranjan");
 console.log(`variable_1 instance of object_1:: ${variable_1 instanceof object_1}`); // variable_1 instance of object_1:: true
 console.log(`variable_2 instance of object_1:: ${variable_2 instanceof object_1}`); // variable_2 instance of object_1:: false
 console.log(`variable_1 instance of object_2:: ${variable_1 instanceof object_2}`); // variable_1 instance of object_2:: false
 console.log(`variable_2 instance of object_2:: ${variable_2 instanceof object_2}`); // variable_2 instance of object_2:: true
 });
 </script>
 <pre>
 // let object_1 = (full_name,date) => {
 // this.full_name = full_name;
 // this.date = date;
 // };
 // above object_1 is not a constructor
 let object_1 = function (full_name,date) {
 this.full_name = full_name;
 this.date = date;
 };
 let variable_1 = new object_1("Ashish Ranjan", "April 06, 2022");
 let object_2 = function (first_name,last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_2 = new object_2("Ashish","Ranjan");
 console.log(`variable_1 instance of object_1:: ${variable_1 instanceof object_1}`); // variable_1 instance of object_1:: true
 console.log(`variable_2 instance of object_1:: ${variable_2 instanceof object_1}`); // variable_2 instance of object_1:: false
 console.log(`variable_1 instance of object_2:: ${variable_1 instanceof object_2}`); // variable_1 instance of object_2:: false
 console.log(`variable_2 instance of object_2:: ${variable_2 instanceof object_2}`); // variable_2 instance of object_2:: true
</pre>

 <hr />
 <hr />
 <h3>own property</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(document).ready(function () {
 let object_1 = function (first_name, last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("ashish1", "ranjan1");
 let variable_2 = new object_1("ashish2", "ranjan2");

 let variable1_properties_array = [];
 for (let property in variable_1) {
 if (variable_1.hasOwnProperty(property)) {
 variable1_properties_array.push(property);
 }
 }
 console.log(`variable1_properties_array:: "${variable1_properties_array}"`); // variable1_properties_array:: "first_name,last_name"
 });
 </script>
 <pre>
 let object_1 = function (first_name,last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("ashish1","ranjan1");
 let variable_2 = new object_1("ashish2","ranjan2");

 let variable1_properties_array = [];
 for (let property in variable_1) {
 if (variable_1.hasOwnProperty(property)) {
 variable1_properties_array.push(property);
 }
 }
 console.log(`variable1_properties_array:: "${variable1_properties_array}"`); // variable1_properties_array:: "first_name,last_name"
</pre>

 <hr />
 <hr />
 <h3>prototype properties to reducew duplicate code</h3>
 <hr />
 <hr />
 All JavaScript objects inherit properties and methods from a prototype.
 The prototype is as a "recipe" for creating objects.
 <script type="text/javascript">
 $(document).ready(function () {
 console.log("\n\nprototype properties to reduce duplicate code");

 let object_1 = function (first_name, last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("Ashish1", "Ranjan1");
 let variable_2 = new object_1("Ashish2", "Ranjan2");
 object_1.prototype.place = "Bengaluru";
 console.log(`variable_1 values = ${JSON.stringify(variable_1)} and prototype place: ${variable_1.place}`); // variable_1 values = {"first_name":"Ashish1","last_name":"Ranjan1"} and prototype place: Bengaluru
 console.log(`variable_2 values = ${JSON.stringify(variable_2)} and prototype place: ${variable_2.place}`); // variable_2 values = {"first_name":"Ashish2","last_name":"Ranjan2"} and prototype place: Bengaluru
 });
 </script>
 <pre>
 let object_1 = function(first_name,last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("Ashish1","Ranjan1");
 let variable_2 = new object_1("Ashish2","Ranjan2");
 object_1.prototype.place = "Bengaluru";
 console.log(`variable_1 values = ${JSON.stringify(variable_1)} and prototype place: ${variable_1.place}`); // variable_1 values = {"first_name":"Ashish1","last_name":"Ranjan1"} and prototype place: Bengaluru
 console.log(`variable_2 values = ${JSON.stringify(variable_2)} and prototype place: ${variable_2.place}`); // variable_2 values = {"first_name":"Ashish2","last_name":"Ranjan2"} and prototype place: Bengaluru
</pre>

 <hr />
 <hr />
 <h3>iterate over object properties</h3>
 <hr />
 <hr />
 <script type="text/javascript">
 $(document).ready(function () {
 console.log("\n\niterate object properties");

 let object_1 = function (first_name, last_name) {
 this.first_name = first_name; // own property
 this.last_name = last_name; // own property
 };
 object_1.prototype.place = "bengaluru"; // prototype property
 object_1.prototype.country = "india"; // prototype property
 let variable_1 = new object_1("ashish", "ranjan");

 let own_property = [];
 let prototype_property = [];
 for (let property in variable_1) {
 if (variable_1.hasOwnProperty(property)) {
 own_property.push(property);
 } else {
 prototype_property.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property)}`); // own properties:: ["first_name","last_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property)}`); // prototype properties:: ["place","country"]
 });
 </script>
 <pre>
 let object_1 = function(first_name,last_name) {
 this.first_name = first_name; // own property
 this.last_name = last_name; // own property
 };
 object_1.prototype.place = "bengaluru"; // prototype property
 object_1.prototype.country = "india"; // prototype property
 let variable_1 = new object_1("ashish","ranjan");

 let own_property = [];
 let prototype_property = [];
 for (let property in variable_1) {
 if (variable_1.hasOwnProperty(property)) {
 own_property.push(property);
 } else {
 prototype_property.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property)}`); // own properties:: ["first_name","last_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property)}`); // prototype properties:: ["place","country"]
</pre>

 <hr />
 <hr />
 <h3>constructor property</h3>
 <hr />
 <hr />
 There is a special constructor property located on the object instances.
 The constructor property is a reference to the constructor function that created the instance.
 <script type="text/javascript">
 $(document).ready(function () {
 console.log("\n\nconstructor property");

 let object_1 = function (first_name, last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("ashish1", "ranjan1");
 let variable_2 = new object_1("ashish2", "ranjan2");

 console.log(variable_1.constructor === object_1); // true
 console.log(variable_2.constructor === object_1); // true
 });
 </script>
 <pre>
 let object_1 = function(first_name,last_name){
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("ashish1","ranjan1");
 let variable_2 = new object_1("ashish2","ranjan2");

 console.log(variable_1.constructor === object_1); // true
 console.log(variable_2.constructor === object_1); // true
</pre>

<hr />
<hr />
<h3>add prototype from new object</h3>
<hr />
<hr />
<script type="text/javascript">
 $(document).ready(function () {
 console.log("add prototype from new object");

 let object_1 = function (first_name, last_name) {
 this.last_name = last_name;
 this.first_name = first_name;
 };

 let variable_1 = new object_1("ashish1", "ranjan1");

 console.log(variable_1, 'variable_1')
 object_1.prototype.place = "bengaluru";
 object_1.prototype.country = "india";
 object_1.prototype.datentime = () => { console.log("today is April 07, 2022"); };
 object_1.prototype.phonenumber = () => { console.log("1234567890"); };

 // console.log(object_1, "object_1")
 let own_property = [];
 let prototype_property = [];
 for (let property in variable_1) {
 console.log(property, 'property')
 if (variable_1.hasOwnProperty(property)) {
 own_property.push(property);
 } else {
 prototype_property.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property)}`); // own properties:: ["last_name","first_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property)}`); // prototype properties:: ["place","country","datentime","phonenumber"]

 object_1.prototype = {
 property_3: "value 3",
 property_4: () => console.log(this.first_name),
 property_5: () => console.log("a2rp: an Ashish Ranjan presentation"),
 };
 let own_property_2 = [];
 let prototype_property_2 = [];
 let variable_2 = new object_1("ashish1", "ranjan1");
 for (let property in variable_2) {
 if (variable_2.hasOwnProperty(property)) {
 own_property_2.push(property);
 } else {
 prototype_property_2.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property_2)}`); // own properties:: ["last_name","first_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property_2)}`); // prototype properties:: ["property_3","property_4","property_5"]
 });
</script>
<pre>
 let object_1 = function (first_name, last_name) {

 // console.log({first_name});
 // console.log({last_name});
 this.last_name = last_name;
 this.first_name = first_name;
 };

 let variable_1 = new object_1("ashish1", "ranjan1");

 console.log(variable_1, 'variable_1')
 object_1.prototype.place = "bengaluru";
 object_1.prototype.country = "india";
 object_1.prototype.datentime = () => { console.log("today is April 07, 2022"); };
 object_1.prototype.phonenumber = () => { console.log("1234567890"); };

 // console.log(object_1, "object_1")
 let own_property = [];
 let prototype_property = [];
 for (let property in variable_1) {
 console.log(property, 'property')
 if (variable_1.hasOwnProperty(property)) {
 own_property.push(property);
 } else {
 prototype_property.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property)}`); // own properties:: ["last_name","first_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property)}`); // prototype properties:: ["place","country","datentime","phonenumber"]

 object_1.prototype = {
 property_3: "value 3",
 property_4: () => console.log(this.first_name),
 property_5: () => console.log("a2rp: an Ashish Ranjan presentation"),
 };
 let own_property_2 = [];
 let prototype_property_2 = [];
 let variable_2 = new object_1("ashish1", "ranjan1");
 for (let property in variable_2) {
 if (variable_2.hasOwnProperty(property)) {
 own_property_2.push(property);
 } else {
 prototype_property_2.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property_2)}`); // own properties:: ["last_name","first_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property_2)}`); // prototype properties:: ["property_3","property_4","property_5"]
</pre>

<hr /><hr />
<h3>set constructor property when changing the prototype</h3>
There is one crucial side effect of manually setting the prototype to a new object. 
It erases the constructor property! 
This property can be used to check which constructor function created the instance, but since the property has been overwritten, it now gives false results:
<script type="text/javascript">
$(document).ready(function(){
 console.log("constructor overwriting");

 let object_1 = function(first_name,last_name) {
 this.first_name = first_name, // own property
 this.last_name = last_name // own property
 };
 let variable_1 = new object_1("ashish1","ranjan1");
 console.log(`variable_1.constructor === object_1:: ${variable_1.constructor === object_1}`); // variable_1.constructor === object_1:: true
 console.log(`variable_1.constructor === Object:: ${variable_1.constructor === Object}`); // variable_1.constructor === Object:: false
 console.log(`variable_1 instanceof object_1:: ${variable_1 instanceof object_1}`); // variable_1 instanceof object_1:: true

 object_1.prototype.place = "bengaluru"; // prototype property

 // prototype property:: all properties in one
 object_1.prototype = {
 constructor: object_1,
 property_1: "value 1",
 property_2: () => console.log("value 2"),
 property_3: () => console.log("value 3"),
 };

 let variable_2 = new object_1("ashish2","ranjan2");

 let own_property_array = [];
 let prototype_property_array = [];
 for (let property in variable_2) {
 if (variable_2.hasOwnProperty(property)) {
 own_property_array.push(property);
 } else {
 prototype_property_array.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property_array)}`); // own properties:: ["first_name","last_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property_array)}`); // prototype properties:: ["constructor","property_1","property_2","property_3"]
 console.log(`the constructor in protype object:: ${variable_2.constructor}`);
 // without constructor in prototype:: the constructor in protype object:: function Object() { [native code] }
 // with constructor in rpototype:: the constructor in protype object:: function(first_name,last_name) {
 // this.first_name = first_name, // own property
 // this.last_name = last_name // own property
 // }
});
</script>
<pre>
 let object_1 = function(first_name,last_name) {
 this.first_name = first_name, // own property
 this.last_name = last_name // own property
 };
 let variable_1 = new object_1("ashish1","ranjan1");
 console.log(`variable_1.constructor === object_1:: ${variable_1.constructor === object_1}`); // variable_1.constructor === object_1:: true
 console.log(`variable_1.constructor === Object:: ${variable_1.constructor === Object}`); // variable_1.constructor === Object:: false
 console.log(`variable_1 instanceof object_1:: ${variable_1 instanceof object_1}`); // variable_1 instanceof object_1:: true

 object_1.prototype.place = "bengaluru"; // prototype property

 // prototype property:: all properties in one
 object_1.prototype = {
 constructor: object_1,
 property_1: "value 1",
 property_2: () => console.log("value 2"),
 property_3: () => console.log("value 3"),
 };

 let variable_2 = new object_1("ashish2","ranjan2");

 let own_property_array = [];
 let prototype_property_array = [];
 for (let property in variable_2) {
 if (variable_2.hasOwnProperty(property)) {
 own_property_array.push(property);
 } else {
 prototype_property_array.push(property);
 }
 }
 console.log(`own properties:: ${JSON.stringify(own_property_array)}`); // own properties:: ["first_name","last_name"]
 console.log(`prototype properties:: ${JSON.stringify(prototype_property_array)}`); // prototype properties:: ["constructor","property_1","property_2","property_3"]
 console.log(`the constructor in protype object:: ${variable_2.constructor}`);
 // without constructor in prototype:: the constructor in protype object:: function Object() { [native code] }
 // with constructor in rpototype:: the constructor in protype object:: function(first_name,last_name) {
 // this.first_name = first_name, // own property
 // this.last_name = last_name // own property
 // }
</pre>

<hr /><hr />
<h3>objects prototype source</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 console.log("objects prototype source");

 let object_1 = function (first_name) {
 this.first_name = first_name;
 };
 let variable_1 = new object_1("ashish");
 console.log(`object_1.prototype.isPrototypeOf(variable_1): ${object_1.prototype.isPrototypeOf(variable_1)}`); // object_1.prototype.isPrototypeOf(variable_1): true
});
</script>
<pre>
 let object_1 = function (first_name) {
 this.first_name = first_name;
 };
 let variable_1 = new object_1("ashish");
 console.log(`object_1.prototype.isPrototypeOf(variable_1): ${object_1.prototype.isPrototypeOf(variable_1)}`); // object_1.prototype.isPrototypeOf(variable_1): true
</pre>

<hr /><hr />
<h3>prototype chain</h3>
<hr /><hr />
All objects in JavaScript (with a few exceptions) have a prototype. 
Also, an object's prototype itself is an object. 
Because a prototype is an object, a prototype can have its own prototype! 
Object is a supertype for all objects in JavaScript. 
Therefore, any object can use the hasOwnProperty method.
<script type="text/javascript">
$(document).ready(function(){
 console.log("prototype chain");

 let object_1 = function(first_name,last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("ashish1","ranjan1");
 console.log(`variable_1.hasOwnProperty("first_name"):: ${variable_1.hasOwnProperty("first_name")}`); // variable_1.hasOwnProperty("first_name"):: true
 console.log(`typeof object_1.prototype:: ${typeof object_1.prototype}`); // typeof object_1.prototype:: object
 console.log(`Object.prototype.isPrototypeOf(Bird.prototype:: ${Object.prototype.isPrototypeOf(object_1.prototype)})`); // Object.prototype.isPrototypeOf(Bird.prototype:: true)
});
</script>
<pre>
 let object_1 = function(first_name,last_name) {
 this.first_name = first_name;
 this.last_name = last_name;
 };
 let variable_1 = new object_1("ashish1","ranjan1");
 console.log(`variable_1.hasOwnProperty("first_name"):: ${variable_1.hasOwnProperty("first_name")}`); // variable_1.hasOwnProperty("first_name"):: true
 console.log(`typeof object_1.prototype:: ${typeof object_1.prototype}`); // typeof object_1.prototype:: object
 console.log(`Object.prototype.isPrototypeOf(Bird.prototype:: ${Object.prototype.isPrototypeOf(object_1.prototype)})`); // Object.prototype.isPrototypeOf(Bird.prototype:: true)
</pre>

<hr /><hr />
<h3>DRY (Dont Repeat Yourself)</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 function Cat(name) {
 this.name = name;
 }

 Cat.prototype = {
 constructor: Cat,
 };

 function Bear(name) {
 this.name = name;
 }

 Bear.prototype = {
 constructor: Bear,
 };

 function Animal() { }

 Animal.prototype = {
 constructor: Animal,
 eat: function() {
 console.log("nom nom nom");
 }
 };
});
</script>
<pre>
 function Cat(name) {
 this.name = name;
 }

 Cat.prototype = {
 constructor: Cat,
 };

 function Bear(name) {
 this.name = name;
 }

 Bear.prototype = {
 constructor: Bear,
 };

 function Animal() { }

 Animal.prototype = {
 constructor: Animal,
 eat: function() {
 console.log("nom nom nom");
 }
 };
</pre>

<hr /><hr />
<h3>Object.create</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function (){
 console.log("\n\nObject.create");

 function Animal() { }
 Animal.prototype = {
 constructor: Animal,
 eat: function() {
 // console.log("nom nom nom");
 return "nom nom nom";
 }
 };

 let duck = Object.create(Animal.prototype);
 console.log(`duck eat:: ${duck.eat()}`); // duck eat:: nom nom nom
 let beagle = Object.create(Animal.prototype);
 console.log(`beagle eat:: ${beagle.eat()}`); // beagle eat:: nom nom nom
});
</script>
<pre>
 function Animal() { }
 Animal.prototype = {
 constructor: Animal,
 eat: function() {
 // console.log("nom nom nom");
 return "nom nom nom";
 }
 };

 let duck = Object.create(Animal.prototype);
 console.log(`duck eat:: ${duck.eat()}`); // duck eat:: nom nom nom
 let beagle = Object.create(Animal.prototype);
 console.log(`beagle eat:: ${beagle.eat()}`); // beagle eat:: nom nom nom
</pre>

<hr /><hr />
<h3>Child's Prototype to an Instance of the Parent</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 function Animal() { }

 Animal.prototype = {
 constructor: Animal,
 eat: function() {
 console.log("nom nom nom");
 }
 };
 function Dog() { }
 
 let beagle = new Dog();
 let animal_instance = new Animal();
 Dog.prototype = Object.create(Animal.prototype);
 console.log(`${(new Dog).eat()}`);
});
</script>
<pre>
 Animal.prototype = {
 constructor: Animal,
 eat: function() {
 console.log("nom nom nom");
 }
 };
 function Dog() { }
 
 let beagle = new Dog();
 let animal_instance = new Animal();
 Dog.prototype = Object.create(Animal.prototype);
 console.log(`${(new Dog).eat()}`);
</pre>

<hr /><hr />
<h3>Reset an Inherited Constructor Property</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 function Animal() { }
 function Bird() { }
 function Dog() { }

 Bird.prototype = Object.create(Animal.prototype);
 Dog.prototype = Object.create(Animal.prototype);

 // Only change code below this line
 Bird.prototype.constructor = Bird;
 Dog.prototype.constructor = Dog;

 let duck = new Bird();
 let beagle = new Dog();
});
</script>
<pre>
 function Animal() { }
 function Bird() { }
 function Dog() { }

 Bird.prototype = Object.create(Animal.prototype);
 Dog.prototype = Object.create(Animal.prototype);

 // Only change code below this line
 Bird.prototype.constructor = Bird;
 Dog.prototype.constructor = Dog;

 let duck = new Bird();
 let beagle = new Dog();
</pre>

<hr /><hr />
<h3>adding methods after inheritance</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 console.log("adding methods after inheritance");

 function Animal() {}
 Animal.prototype.eat = function () {
 console.log("nom nom nom");
 };

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 Bird.prototype.constructor = Bird;
 let bird_object = new Bird();
 let own_property = [];
 let prototype_property = [];
 // for (let property in bird_object) {
 // if (bird_object.hasOwnProperty(property) {
 // own_property.push(property);
 // } else {
 // prototype_property.push(property);
 // }
 // }
 // console.log(`own property ${own_property} prototype property: ${prototype_property}`);
});
</script>

<hr /><hr />
<h3>set the child prototype to instance of parent</h3>
<hr /><hr /><script type="text/javascript">
$(document).ready(function(){
 console.log("\n\nset child prototype to that of parent");

 function Animal() {}
 Animal.prototype = {
 sound_1: "bark",
 sound_2: "speak",
 };

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);

 let bird_variable = new Bird();
 let own_property_array = [];
 let prototype_property_array = [];
 for (let property in bird_variable) {
 if (bird_variable.hasOwnProperty(property)) {
 own_property.push(property);
 } else {
 prototype_property_array.push(property);
 }
 }
 console.log(`own_property ${own_property_array} prototype_property ${prototype_property_array}`); // own_property prototype_property sound_1,sound_2
});
</script>
<pre>
 function Animal() {}
 Animal.prototype = {
 sound_1: "bark",
 sound_2: "speak",
 };

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);

 let bird_variable = new Bird();
 let own_property_array = [];
 let prototype_property_array = [];
 for (let property in bird_variable) {
 if (bird_variable.hasOwnProperty(property)) {
 own_property.push(property);
 } else {
 prototype_property_array.push(property);
 }
 }
 console.log(`own_property ${own_property_array} prototype_property ${prototype_property_array}`); // own_property prototype_property sound_1,sound_2
</pre>

<hr /><hr />
<h3>reset an inherited constructor property</h3>
<hr /><hr />
When an object inherits its prototype from another object, it also inherits the supertype's constructor property.

<script type="text/javascript">
$(document).ready(function (){
 console.log("reset an inherited constructor property");

 function Animal() {}
 Animal.prototype.make_noise = "bark";

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 let duck = new Bird();
 console.log(duck.constructor); // ƒ Animal() {}

 Bird.prototype.constructor = Bird;
 console.log(duck.constructor); // ƒ Bird() {}
});
</script>
<pre>
 function Animal() {}
 Animal.prototype.make_noise = "bark";

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 let duck = new Bird();
 console.log(duck.constructor); // ƒ Animal() {}

 Bird.prototype.constructor = Bird;
 console.log(duck.constructor); // ƒ Bird() {}
</pre>

<hr /><hr />
<h3>add methods after inheritance</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 console.log("\n\nadd method after inheritance");

 function Animal() {}
 Animal.prototype.eat = function() {
 console.log("eaten/ate");
 }
 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 let bird_object_1 = new Bird();
 console.log(`bird_object_1.constructor:: ${bird_object_1.constructor}`); // bird_object_1.constructor:: function Animal() {}

 Bird.prototype.constructor = Bird;
 let bird_object_2 = new Bird();
 console.log(`bird_object_2.constructor:: ${bird_object_2.constructor}`); // bird_object_2.constructor:: function Bird() {}

 Bird.prototype.fly = function() {
 console.log("m flying");
 };
 let object_3 = new Bird();
 object_3.eat(); // eaten/ate
 object_3.fly() // m flying
});
</script>
<pre>
 function Animal() {}
 Animal.prototype.eat = function() {
 console.log("eaten/ate");
 }
 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 let bird_object_1 = new Bird();
 console.log(`bird_object_1.constructor:: ${bird_object_1.constructor}`); // bird_object_1.constructor:: function Animal() {}

 Bird.prototype.constructor = Bird;
 let bird_object_2 = new Bird();
 console.log(`bird_object_2.constructor:: ${bird_object_2.constructor}`); // bird_object_2.constructor:: function Bird() {}

 Bird.prototype.fly = function() {
 console.log("m flying");
 };
 let object_3 = new Bird();
 object_3.eat(); // eaten/ate
 object_3.fly() // m flying
</pre>

<hr /><hr />
<h3>Override Inherited Methods</h3>
<hr /><hr />
An object can inherit its behavior (methods) from another object by referencing its prototype object:
<pre>ChildObject.prototype = Object.create(ParentObject.prototype);</pre>
The ChildObject received its own methods by chaining them onto its prototype:
<pre>ChildObject.prototype.methodName = function() {...};</pre>
<script type="text/javascript">
$(document).ready(function(){
 console.log("\n\noverride inherited methods");

 function Animal() {}
 Animal.prototype.eat = function() { console.log("eat sound"); };

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 let bird_object_1 = new Bird();
 bird_object_1.eat(); // eat sound

 Bird.prototype.eat = function() { console.log("sound of bird eating"); };
 bird_object_1.eat(); // sound of bird eating
});
</script>
<pre>
 function Animal() {}
 Animal.prototype.eat = function() { console.log("eat sound"); };

 function Bird() {}
 Bird.prototype = Object.create(Animal.prototype);
 let bird_object_1 = new Bird();
 bird_object_1.eat(); // eat sound

 Bird.prototype.eat = function() { console.log("sound of bird eating"); };
 bird_object_1.eat(); // sound of bird eating
</pre>

<hr /><hr />
<h3>Use a Mixin to Add Common Behavior Between Unrelated Objects</h3>
<hr /><hr />
<script type="text/javascript">
$(document).ready(function(){
 console.log("\n\nmixin to add common behavior to unrelated objects");

 let fly_mixin = function(obj) {
 obj.fly = function() { console.log("m flying"); };
 }
 let bird = {
 name: "Donald",
 count_legs: 2,
 };
 let plane = {
 model: 777,
 count_passengers: 123,
 };
 fly_mixin(bird);
 fly_mixin(plane);
 bird.fly();
});
</script>



</div>