Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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"]
- */
Add Comment
Please, Sign In to add comment