Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created July 24, 2019 15:29
Show Gist options
  • Save prof3ssorSt3v3/4d3424cbb30a37133e91eb24467eb218 to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/4d3424cbb30a37133e91eb24467eb218 to your computer and use it in GitHub Desktop.
/**
* linking prototype objects to build a prototype chain
* __proto__ vs Object.getPrototypeOf(obj) & Object.setPrototypeOf(obj)
*/
// 1. obj --> otherProto.prototype --> Object.prototype --> null
let otherProto = function() {
this.prop1 = 456; // this means the instance of the object we are creating
this.inner = function() {
console.log("inner method on instance");
};
//automatically returns the instance of the object
};
otherProto.prototype.someMethod = function() {
console.log("this is otherProto");
};
let obj = new otherProto();
// console.log(obj.prop1); // 456
// obj.inner();
// obj.someMethod();
// obj.toString();
// obj.__proto__.inner(); // fail
// Object.getPrototypeOf(obj).inner(); // fail
// Object.getPrototypeOf(obj).someMethod(); //yes
// 2.protoObj --> Object.prototype --> null
let protoObj = {
prop1: 456,
someMethod: function() {
console.log("this is someMethod");
}
}; // let protoObj = new Object(); protoObj.prop1 = 456; protoObj.someMethod = function(){}
//Object.getPrototypeOf(protoObj).otherMethod = function(){}
protoObj.__proto__.otherMethod = function() {
console.log("this is otherMethod");
};
//3. childObj ---> protoObj ---> Object.prototype --> null
let childObj = {};
Object.setPrototypeOf(childObj, protoObj);
// console.log(childObj.prop1);
// childObj.someMethod();
// childObj.otherMethod();
// childObj.nonmethod();
// 4. childObj2 ---> protoObj ---> Object.prototype --> null
let childObj2 = Object.create(protoObj);
console.log(childObj2.prop1); //456 coming from protoObj
childObj2.prop1 = 777; // created a new property inside childObj2 called prop1
console.log(childObj2.prop1, childObj2.__proto__.prop1);
childObj2.someMethod(); //calls the one inside protoObj
childObj2.someMethod = function() {
console.log("new method inside childObj2");
};
childObj2.someMethod();
childObj2.__proto__.someMethod();
@MdSaifulIslam
Copy link

Thank you, so much.
It's good understanding in one video.

@rizkysr90
Copy link

Thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment