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