Saturday, September 11, 2021

Another ways to create object using Object.create()

 showing object creating using Object.create() using the following example,

var personProto={

    calculateAge:function(){

        console.log(2021-this.yearOfBirth);

    }

}

var john = Object.create(personProto);

john.name="john smith";

john.yearOfBirth=1989;

john.job = "engineer";


console.log(john);

john.calculateAge();

here ,personProto is a prototype which i am using to create an object,

var john = Object.create(personProto);

we first create john object then we define properties.

Another way to create an object-

var john = Object.create(personProto, {

    name: {

        value: "john"

    },

    yearOfBirth: {

        value: 1989

    },

    job: {

        value: "engineer"

    }

});

john.calculateAge();

the difference with function constructor is that , we inherit the property when we creating the object .


No comments:

Post a Comment

Element of a good table (Ref: Database design mere mortals by Michael J. Hernandez)

  Elements of the Ideal Table: It represents a single subject, which can be an object or event that reduces the risk of potential data integ...