Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /*prototype introduction:
  12.  
  13. 1: all JS object has a default protptype called:
  14. Object.prototype which is an object has lots of built-in methods: to String:
  15. */
  16.  
  17. /*
  18. var person = {}
  19. Object.prototype.name = "david";
  20. console.log(person.name);
  21.  
  22. var p20obj = {}
  23. console.log(p20obj);
  24. */
  25.  
  26. /*prototype chain! :
  27. first starts from object itself, if cant find, go to object's prototype,
  28. if can't find, go to prototype of ocject's prototype, until goes to the ROOT
  29. Object.prototype, if still can't find, then throw error */
  30. var person ={
  31. name : "person",
  32. age :20
  33. }
  34.  
  35. var davidobj = Object.create(person);
  36. davidobj.name = "david";
  37. Object.prototype.name = "object prototype";
  38.  
  39. /* console.log(davidobj.name);
  40. console.log(person.name);
  41. console.log(Object.prototype.name);
  42. */
  43.  
  44. //Object.getPrototypeOf(obj) : get the prototype of obj
  45.  
  46. /*console.log(Object.getPrototypeOf(davidobj) == person) ; //true
  47. console.log(Object.getPrototypeOf(davidobj) == Object.prototype) ; //false*/
  48.  
  49. /*constructor
  50. when you call the function, it returns you an object*/
  51. function Person(name, age){
  52. this.name = name;
  53. this.age = age;
  54. }
  55.  
  56. var davidObj = new Person("david", 30);
  57. console.log(davidObj);
  58. </script>
  59.  
  60.  
  61.  
  62. <script id="jsbin-source-javascript" type="text/javascript">/*prototype introduction:
  63.  
  64. 1: all JS object has a default protptype called:
  65. Object.prototype which is an object has lots of built-in methods: to String:
  66. */
  67.  
  68. /*
  69. var person = {}
  70. Object.prototype.name = "david";
  71. console.log(person.name);
  72.  
  73. var p20obj = {}
  74. console.log(p20obj);
  75. */
  76.  
  77. /*prototype chain! :
  78. first starts from object itself, if cant find, go to object's prototype,
  79. if can't find, go to prototype of ocject's prototype, until goes to the ROOT
  80. Object.prototype, if still can't find, then throw error */
  81. var person ={
  82. name : "person",
  83. age :20
  84. }
  85.  
  86. var davidobj = Object.create(person);
  87. davidobj.name = "david";
  88. Object.prototype.name = "object prototype";
  89.  
  90. /* console.log(davidobj.name);
  91. console.log(person.name);
  92. console.log(Object.prototype.name);
  93. */
  94.  
  95. //Object.getPrototypeOf(obj) : get the prototype of obj
  96.  
  97. /*console.log(Object.getPrototypeOf(davidobj) == person) ; //true
  98. console.log(Object.getPrototypeOf(davidobj) == Object.prototype) ; //false*/
  99.  
  100. /*constructor
  101. when you call the function, it returns you an object*/
  102. function Person(name, age){
  103. this.name = name;
  104. this.age = age;
  105. }
  106.  
  107. var davidObj = new Person("david", 30);
  108. console.log(davidObj);
  109.  
  110. </script></body>
  111. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement