Guest User

Untitled

a guest
Feb 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. 啊~怎么回事,睡了好久啊~
  2.  
  3. 诶?我是谁?我在哪?我在干什么啊?
  4.  
  5. 我怎么在电脑旁打字?
  6.  
  7. 诶嘿?已经最后一篇了?
  8.  
  9. wow~WOW~wow~
  10.  
  11. 那么,让我们开始最后一篇吧!!
  12.  
  13. 又是干货满满的一篇呢!
  14.  
  15. ```
  16.  
  17. JavaScript 中,万物皆对象!但对象也是有区别的。分为普通对象和函数对象,Object 、Function 是 JS 自带的函数对象。下面举例说明
  18. var o1 = {};
  19. var o2 =new Object();
  20. var o3 = new f1();
  21.  
  22. function f1(){};
  23. var f2 = function(){};
  24. var f3 = new Function('str','console.log(str)');
  25.  
  26. console.log(typeof Object); //function
  27. console.log(typeof Function); //function
  28.  
  29. console.log(typeof f1); //function
  30. console.log(typeof f2); //function
  31. console.log(typeof f3); //function
  32.  
  33. console.log(typeof o1); //object
  34. console.log(typeof o2); //object
  35. console.log(typeof o3); //object
  36.  
  37. 在上面的例子中 o1 o2 o3 为普通对象,f1 f2 f3 为函数对象。怎么区分,其实很简单,凡是通过 new Function() 创建的对象都是函数对象,其他的都是普通对象。f1,f2,归根结底都是通过 new Function()的方式进行创建的。Function Object 也都是通过 New Function()创建的。
  38. 一定要分清楚普通对象和函数对象,下面我们会常常用到它。
  39.  
  40. 我们先复习一下构造函数的知识:
  41. function Person(name, age, job) {
  42. this.name = name;
  43. this.age = age;
  44. this.job = job;
  45. this.sayName = function() { alert(this.name) }
  46. }
  47. var person1 = new Person('Zaxlct', 28, 'Software Engineer');
  48. var person2 = new Person('Mick', 23, 'Doctor');
  49.  
  50. 上面的例子中 person1 和 person2 都是 Person 的实例。这两个实例都有一个 constructor (构造函数)属性,该属性(是一个指针)指向 Person。 即:
  51. console.log(person1.constructor == Person); //true
  52. console.log(person2.constructor == Person); //true
  53.  
  54. 我们要记住两个概念(构造函数,实例):
  55. person1 和 person2 都是 构造函数 Person 的实例
  56. 一个公式:
  57. 实例的构造函数属性(constructor)指向构造函数。
  58.  
  59. 在 JavaScript 中,每当定义一个对象(函数也是对象)时候,对象中都会包含一些预定义的属性。其中每个函数对象都有一个prototype 属性,这个属性指向函数的原型对象。(先用不管什么是 __proto__ 第二节的课程会详细的剖析)
  60. function Person() {}
  61. Person.prototype.name = 'Zaxlct';
  62. Person.prototype.age = 28;
  63. Person.prototype.job = 'Software Engineer';
  64. Person.prototype.sayName = function() {
  65. alert(this.name);
  66. }
  67.  
  68. var person1 = new Person();
  69. person1.sayName(); // 'Zaxlct'
  70.  
  71. var person2 = new Person();
  72. person2.sayName(); // 'Zaxlct'
  73.  
  74. console.log(person1.sayName == person2.sayName); //true
  75.  
  76. 我们得到了本文第一个「定律」:
  77. 每个对象都有 __proto__ 属性,但只有函数对象才有 prototype 属性
  78.  
  79.  
  80. 那什么是原型对象呢?
  81. 我们把上面的例子改一改你就会明白了:
  82. Person.prototype = {
  83. name: 'Zaxlct',
  84. age: 28,
  85. job: 'Software Engineer',
  86. sayName: function() {
  87. alert(this.name);
  88. }
  89. }
  90.  
  91. 原型对象,顾名思义,它就是一个普通对象(废话 = =!)。从现在开始你要牢牢记住原型对象就是 Person.prototype ,如果你还是害怕它,那就把它想想成一个字母 A: var A = Person.prototype
  92.  
  93. 在上面我们给 A 添加了 四个属性:name、age、job、sayName。其实它还有一个默认的属性:constructor
  94.  
  95. 在默认情况下,所有的原型对象都会自动获得一个 constructor(构造函数)属性,这个属性(是一个指针)指向 prototype 属性所在的函数(Person)
  96.  
  97. 上面这句话有点拗口,我们「翻译」一下:A 有一个默认的 constructor 属性,这个属性是一个指针,指向 Person。即:
  98. Person.prototype.constructor == Person
  99.  
  100. 在上面第二小节《构造函数》里,我们知道实例的构造函数属性(constructor)指向构造函数 :person1.constructor == Person
  101. 这两个「公式」好像有点联系:
  102. person1.constructor == Person
  103. Person.prototype.constructor == Person
  104.  
  105. person1 为什么有 constructor 属性?那是因为 person1 是 Person 的实例。
  106. 那 Person.prototype 为什么有 constructor 属性??同理, Person.prototype (你把它想象成 A) 也是Person 的实例。
  107. 也就是在 Person 创建的时候,创建了一个它的实例对象并赋值给它的 prototype,基本过程如下:
  108. var A = new Person();
  109. Person.prototype = A;
  110.  
  111. 结论:原型对象(Person.prototype)是 构造函数(Person)的一个实例。
  112.  
  113. 原型对象其实就是普通对象(但 Function.prototype 除外,它是函数对象,但它很特殊,他没有prototype属性(前面说道函数对象都有prototype属性))。看下面的例子:
  114. function Person(){};
  115. console.log(Person.prototype) //Person{}
  116. console.log(typeof Person.prototype) //Object
  117. console.log(typeof Function.prototype) // Function,这个特殊
  118. console.log(typeof Object.prototype) // Object
  119. console.log(typeof Function.prototype.prototype) //undefined
  120.  
  121. Function.prototype 为什么是函数对象呢?
  122. var A = new Function ();
  123. Function.prototype = A;
  124.  
  125. 上文提到凡是通过 new Function( ) 产生的对象都是函数对象。因为 A 是函数对象,所以Function.prototype 是函数对象。
  126. 那原型对象是用来做什么的呢?主要作用是用于继承。举个例子:
  127. var Person = function(name){
  128. this.name = name; // tip: 当函数执行时这个 this 指的是谁?
  129. };
  130. Person.prototype.getName = function(){
  131. return this.name; // tip: 当函数执行时这个 this 指的是谁?
  132. }
  133. var person1 = new person('Mick');
  134. person1.getName(); //Mick
  135.  
  136. 从这个例子可以看出,通过给 Person.prototype 设置了一个函数对象的属性,那有 Person 的实例(person1)出来的普通对象就继承了这个属性。具体是怎么实现的继承,就要讲到下面的原型链了。
  137. 小问题,上面两个 this 都指向谁?
  138. var person1 = new person('Mick');
  139. person1.name = 'Mick'; // 此时 person1 已经有 name 这个属性了
  140. person1.getName(); //Mick
  141.  
  142. 故两次 this 在函数执行时都指向 person1。
  143.  
  144. ```
  145.  
  146. 作业完成喽~哇呀呀呀呀呀呀~嘿!
  147.  
  148. see you again!
Add Comment
Please, Sign In to add comment