Skip to content

Instantly share code, notes, and snippets.

@sevperez
Created July 18, 2018 18:18
Show Gist options
  • Save sevperez/244a2a659c4c59e00778a18bfaa2b557 to your computer and use it in GitHub Desktop.
Save sevperez/244a2a659c4c59e00778a18bfaa2b557 to your computer and use it in GitHub Desktop.
// prototypes5.js
function crawlPrototypeChain(obj) {
console.log("-----------------");
console.log("Object: ", obj);
console.log("Own Properties: ", Object.getOwnPropertyNames(obj));
var objPrototype = Object.getPrototypeOf(obj);
if (objPrototype) {
crawlPrototypeChain(objPrototype);
}
}
var House = {
ringDoorbell: function() {
console.log("ding dong!");
},
describe: function() {
console.log(this.owner + "'s house has " + this.rooms + " rooms.");
}
};
var Castle = Object.create(House);
Castle.describe = function() {
console.log(this.owner + " owns a castle with " + this.rooms + " rooms!");
};
var hearstCastle = Object.create(Castle);
hearstCastle.owner = "William Randolph Hearst";
hearstCastle.rooms = 56;
crawlPrototypeChain(hearstCastle);
/* LOGS:
-----------------
Object: {owner: "William Randolph Hearst", rooms: 56} // hearstCastle
Own Properties: ["owner", "rooms"]
-----------------
Object: {describe: ƒ} // Castle
Own Properties: ["describe"]
-----------------
Object: {ringDoorbell: ƒ, describe: ƒ} // House
Own Properties: ["ringDoorbell", "describe"]
-----------------
Object: {constructor: ƒ, // Object.prototype
__defineGetter__: ƒ,
__defineSetter__: ƒ,
hasOwnProperty: ƒ,
__lookupGetter__: ƒ,
… }
Own Properties: ["constructor", "__defineGetter__", "__defineSetter__",
"hasOwnProperty", "__lookupGetter__", "__lookupSetter__",
"isPrototypeOf", "propertyIsEnumerable", "toString", "valueOf",
"__proto__", "toLocaleString"]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment