Guest User

Untitled

a guest
Jul 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. // prototypes5.js
  2.  
  3. function crawlPrototypeChain(obj) {
  4. console.log("-----------------");
  5. console.log("Object: ", obj);
  6. console.log("Own Properties: ", Object.getOwnPropertyNames(obj));
  7.  
  8. var objPrototype = Object.getPrototypeOf(obj);
  9. if (objPrototype) {
  10. crawlPrototypeChain(objPrototype);
  11. }
  12. }
  13.  
  14. var House = {
  15. ringDoorbell: function() {
  16. console.log("ding dong!");
  17. },
  18.  
  19. describe: function() {
  20. console.log(this.owner + "'s house has " + this.rooms + " rooms.");
  21. }
  22. };
  23.  
  24. var Castle = Object.create(House);
  25. Castle.describe = function() {
  26. console.log(this.owner + " owns a castle with " + this.rooms + " rooms!");
  27. };
  28.  
  29. var hearstCastle = Object.create(Castle);
  30. hearstCastle.owner = "William Randolph Hearst";
  31. hearstCastle.rooms = 56;
  32.  
  33. crawlPrototypeChain(hearstCastle);
  34.  
  35. /* LOGS:
  36.  
  37. -----------------
  38. Object: {owner: "William Randolph Hearst", rooms: 56} // hearstCastle
  39. Own Properties: ["owner", "rooms"]
  40.  
  41. -----------------
  42. Object: {describe: ƒ} // Castle
  43. Own Properties: ["describe"]
  44.  
  45. -----------------
  46. Object: {ringDoorbell: ƒ, describe: ƒ} // House
  47. Own Properties: ["ringDoorbell", "describe"]
  48.  
  49. -----------------
  50. Object: {constructor: ƒ, // Object.prototype
  51. __defineGetter__: ƒ,
  52. __defineSetter__: ƒ,
  53. hasOwnProperty: ƒ,
  54. __lookupGetter__: ƒ,
  55. … }
  56. Own Properties: ["constructor", "__defineGetter__", "__defineSetter__",
  57. "hasOwnProperty", "__lookupGetter__", "__lookupSetter__",
  58. "isPrototypeOf", "propertyIsEnumerable", "toString", "valueOf",
  59. "__proto__", "toLocaleString"]
  60.  
  61. */
Add Comment
Please, Sign In to add comment