Skip to main content
⏱ 0:00est. 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();
};
}