β± 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();
};
}