⏱ 0:01est. 4 min
Chapter 63- Object.create and Pure Prototypal Inheritance
Notes
Example 1 : //Pure Prototypal Inheritance
//base object
var person = {
firstname : ‘Default',
lastname : ‘Default,
greet : function(){
return ‘Hi ' + this.firstname;
}
}
var john = Object.create(person);
//This will return an empty object with __proto__ having all properties
console.log(john);
john.greet(); // "Hi Default"
//To hide old values
john.firstname = ‘John';
john.lastname = ‘Doe';
console.log(john);
john.greet(); // "Hi John"
#BIGWORD - Polyfill : Code that adds a feature which an engine may lack.
Example 1 :
var person = {
firstname : ‘Default',
lastname : ‘Default,
greet : function(){
return ‘Hi ' + this.firstname;
}
}
// Providing support to browsers which don't have Object.create feature
var john = Object.create(person);
//Solving problem with polyfill
if(!Object.create){
Object.create = function(o){
if(arguments.length>1){
throw new Error (‘Object.create impl accepts only first parameter');
}
function F() {}
F.prototype = o;
return new F();
};
}