1. this
  2. this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window; 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用。 我们还可以使用apply和call两个全局方法来改变函数中this的具体指向。
  3. 先看一个在全局作用范围内使用this的例子:
  4. <script type="text/javascript">
  5. console.log(this === window);  // true
  6. console.log(window.alert === this.alert);  // true
  7. console.log(this.parseInt("021", 10));  // 10
  8. </script>
  9. 函数中的this是在运行时决定的,而不是函数定义时,如下:
  10. // 定义一个全局函数
  11. function foo() {
  12. console.log(this.fruit);
  13. }
  14. // 定义一个全局变量,等价于window.fruit = "apple";
  15. var fruit = "apple";
  16. // 此时函数foo中this指向window对象
  17. // 这种调用方式和window.foo();是完全等价的
  18. foo();  // "apple"
  19. // 自定义一个对象,并将此对象的属性foo指向全局函数foo
  20. var pack = {
  21. fruit: "orange",
  22. foo: foo
  23. };
  24. // 此时函数foo中this指向window.pack对象
  25. pack.foo(); // "orange"
  26. 全局函数apply和call可以用来改变函数中this的指向,如下:
  27. // 定义一个全局函数
  28. function foo() {
  29. console.log(this.fruit);
  30. }
  31. // 定义一个全局变量
  32. var fruit = "apple";
  33. // 自定义一个对象
  34. var pack = {
  35. fruit: "orange"
  36. };
  37. // 等价于window.foo();
  38. foo.apply(window);  // "apple"
  39. // 此时foo中的this === pack
  40. foo.apply(pack);    // "orange"
  41. 注:apply和call两个函数的作用相同,唯一的区别是两个函数的参数定义不同。
  42. 因为在JavaScript中函数也是对象,所以我们可以看到如下有趣的例子:
  43. // 定义一个全局函数
  44. function foo() {
  45. if (this === window) {
  46. console.log("this is window.");
  47. }
  48. }
  49. // 函数foo也是对象,所以可以定义foo的属性boo为一个函数
  50. foo.boo = function() {
  51. if (this === foo) {
  52. console.log("this is foo.");
  53. } else if (this === window) {
  54. console.log("this is window.");
  55. }
  56. };
  57. // 等价于window.foo();
  58. foo();  // this is window.
  59. // 可以看到函数中this的指向调用函数的对象
  60. foo.boo();  // this is foo.
  61. // 使用apply改变函数中this的指向
  62. foo.boo.apply(window);  // this is window.
  63. prototype
  64. 我们已经在第一章中使用prototype模拟类和继承的实现。 prototype本质上还是一个JavaScript对象。 并且每个函数都有一个默认的prototype属性。
  65. 如果这个函数被用在创建自定义对象的场景中,我们称这个函数为构造函数。 比如下面一个简单的场景:
  66. // 构造函数
  67. function Person(name) {
  68. this.name = name;
  69. }
  70. // 定义Person的原型,原型中的属性可以被自定义对象引用
  71. Person.prototype = {
  72. getName: function() {
  73. return this.name;
  74. }
  75. }
  76. var zhang = new Person("ZhangSan");
  77. console.log(zhang.getName());   // "ZhangSan"
  78. 作为类比,我们考虑下JavaScript中的数据类型 - 字符串(String)、数字(Number)、数组(Array)、对象(Object)、日期(Date)等。 我们有理由相信,在JavaScript内部这些类型都是作为构造函数来实现的,比如:
  79. // 定义数组的构造函数,作为JavaScript的一种预定义类型
  80. function Array() {
  81. // ...
  82. }
  83. // 初始化数组的实例
  84. var arr1 = new Array(1, 56, 34, 12);
  85. // 但是,我们更倾向于如下的语法定义:
  86. var arr2 = [1, 56, 34, 12];
  87. 同时对数组操作的很多方法(比如concat、join、push)应该也是在prototype属性中定义的。
  88. 实际上,JavaScript所有的固有数据类型都具有只读的prototype属性(这是可以理解的:因为如果修改了这些类型的prototype属性,则哪些预定义的方法就消失了), 但是我们可以向其中添加自己的扩展方法。
  89. // 向JavaScript固有类型Array扩展一个获取最小值的方法
  90. Array.prototype.min = function() {
  91. var min = this[0];
  92. for (var i = 1; i < this.length; i++) {
  93. if (this[i] < min) {
  94. min = this[i];
  95. }
  96. }
  97. return min;
  98. };
  99. // 在任意Array的实例上调用min方法
  100. console.log([1, 56, 34, 12].min());  // 1
  101. 注意:这里有一个陷阱,向Array的原型中添加扩展方法后,当使用for-in循环数组时,这个扩展方法也会被循环出来。
  102. 下面的代码说明这一点(假设已经向Array的原型中扩展了min方法):
  103. var arr = [1, 56, 34, 12];
  104. var total = 0;
  105. for (var i in arr) {
  106. total += parseInt(arr[i], 10);
  107. }
  108. console.log(total);   // NaN
  109. 解决方法也很简单:
  110. var arr = [1, 56, 34, 12];
  111. var total = 0;
  112. for (var i in arr) {
  113. if (arr.hasOwnProperty(i)) {
  114. total += parseInt(arr[i], 10);
  115. }
  116. }
  117. console.log(total);   // 103
  118. constructor
  119. constructor始终指向创建当前对象的构造函数。比如下面例子:
  120. // 等价于 var foo = new Array(1, 56, 34, 12);
  121. var arr = [1, 56, 34, 12];
  122. console.log(arr.constructor === Array); // true
  123. // 等价于 var foo = new Function();
  124. var Foo = function() { };
  125. console.log(Foo.constructor === Function); // true
  126. // 由构造函数实例化一个obj对象
  127. var obj = new Foo();
  128. console.log(obj.constructor === Foo); // true
  129. // 将上面两段代码合起来,就得到下面的结论
  130. console.log(obj.constructor.constructor === Function); // true
  131. 但是当constructor遇到prototype时,有趣的事情就发生了。
  132. 我们知道每个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。如下例所示:
  133. function Person(name) {
  134. this.name = name;
  135. };
  136. Person.prototype.getName = function() {
  137. return this.name;
  138. };
  139. var p = new Person("ZhangSan");
  140. console.log(p.constructor === Person);  // true
  141. console.log(Person.prototype.constructor === Person); // true
  142. // 将上两行代码合并就得到如下结果
  143. console.log(p.constructor.prototype.constructor === Person); // true
  144. 当时当我们重新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖), constructor的行为就有点奇怪了,如下示例:
  145. function Person(name) {
  146. this.name = name;
  147. };
  148. Person.prototype = {
  149. getName: function() {
  150. return this.name;
  151. }
  152. };
  153. var p = new Person("ZhangSan");
  154. console.log(p.constructor === Person);  // false
  155. console.log(Person.prototype.constructor === Person); // false
  156. console.log(p.constructor.prototype.constructor === Person); // false
  157. 为什么呢?
  158. 原来是因为覆盖Person.prototype时,等价于进行如下代码操作:
  159. Person.prototype = new Object({
  160. getName: function() {
  161. return this.name;
  162. }
  163. });
  164. 而constructor始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:
  165. function Person(name) {
  166. this.name = name;
  167. };
  168. Person.prototype = {
  169. getName: function() {
  170. return this.name;
  171. }
  172. };
  173. var p = new Person("ZhangSan");
  174. console.log(p.constructor === Object);  // true
  175. console.log(Person.prototype.constructor === Object); // true
  176. console.log(p.constructor.prototype.constructor === Object); // true
  177. 怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:
  178. function Person(name) {
  179. this.name = name;
  180. };
  181. Person.prototype = new Object({
  182. getName: function() {
  183. return this.name;
  184. }
  185. });
  186. Person.prototype.constructor = Person;
  187. var p = new Person("ZhangSan");
  188. console.log(p.constructor === Person);  // true
  189. console.log(Person.prototype.constructor === Person); // true
  190. console.log(p.constructor.prototype.constructor === Person); // true

javascript实现继承的方式的更多相关文章

  1. JavaScript实现继承的方式和各自的优缺点

    ECMAscript只支持实现继承,主要是依靠原型链来实现的. JavaScript实现继承的方式: 类式继承 构造函数继承 组合继承 寄生组合式继承 1.类式继承 //类式继承 //声明父类 fun ...

  2. javascript 中继承实现方式归纳

    转载自:http://sentsin.com/web/1109.html 不同于基于类的编程语言,如 C++ 和 Java,javascript 中的继承方式是基于原型的.同时由于 javascrip ...

  3. JavaScript的继承实现方式

    1.使用call或apply方法,将父对象的构造函数绑定在子对象上 function A(){ this.name = 'json'; } function B(){ A.call(this); } ...

  4. JavaScript 的继承与多态

    本文先对es6发布之前javascript各种继承实现方式进行深入的分析比较,然后再介绍es6中对类继承的支持以及优缺点讨论.最后介绍了javascript面向对象编程中很少被涉及的“多态”,并提供了 ...

  5. javascript实现继承的几种方式

    原型链方式实现继承 function SuperType(){ this.property = true; this.colors = ['red','blue','green']; } SuperT ...

  6. DOM笔记(十三):JavaScript的继承方式

    在Java.C++.C#等OO语言中,都支持两种继承方式:接口继承和实现继承.接口继承制继承方法签名,实现继承则继承实际的方法和属性.在SCMAScript中,由于函数没有签名,所以无法实现接口继承, ...

  7. 【编程题与分析题】Javascript 之继承的多种实现方式和优缺点总结

    [!NOTE] 能熟练掌握每种继承方式的手写实现,并知道该继承实现方式的优缺点. 原型链继承 function Parent() { this.name = 'zhangsan'; this.chil ...

  8. javascript继承(七)—用继承的方式实现照片墙功能

    照片墙DEMO下载 注意:图片有四种类型:1可放大:2可拖动:3既可放大也可拖动:4都不行.由于每个图片的构造函数不同而不同(目前在火狐上调试的,其它的浏览器可能不行,请见谅,主要讲继承的思想.以后会 ...

  9. Javascript之继承(原型链方式)

    1.原型链 原型链是JavaScript中继承的主要方法. 每个构造函数都拥有一个原型对象,原型对象都包含一个指向构造函数的指针(constructor),实例都包含一个指向原型对象的内部指针(__p ...

随机推荐

  1. Provider 错误 '80004005' 未指定的错误 的最终解决方法

    今天在配置公司的香港WEB服务器Server2003系统,建好应用程序池后,发现远行程序经常出现下面的错误,刷新几下又可以,但过不了多久又是出现下面的错误!! 在网上查找相关问题得知,这是2003SP ...

  2. VBS学习:流程控制语句判断结构

    一.数值运算: 1) Dim a,b,c a=inputbox("a是:","输入半径") b=Inputbox("b是:","输 ...

  3. flappy pig小游戏源码分析(1)——主程序初探

    闲逛github发现一个javascript原生实现的小游戏,源码写的很清晰,适合想提高水平的同学观摩学习.读通源码后,我决定写一系列的博客来分析源码,从整体架构到具体实现细节来帮助一些想提高水平的朋 ...

  4. [转] AOP面向切面编程

    AOP面向切面编程 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  5. Spring Batch的事务-Part 1:基础

    原文 https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/ This is th ...

  6. 【24点游戏】cocos2dx 源码

    1.  4个数字 24点判断 double Calc(double a, double b, string oper) { double result = 0; const char *p = ope ...

  7. pybombs 安装

    参考:https://github.com/gnuradio/pybombs 先装:pip 然后: pip install PyBOMBS 更新源: pybombs recipes add gr-re ...

  8. collectionView/tableview刷新时关闭动画无效

    collectionView/tableview reloadSections/reloaddata时去掉动画无效时可以尝试使用 [UIView performWithoutAnimation:^{ ...

  9. MoveTo和MoveBy

    cc.MoveTo是“移动到这里",而cc.MoveBy则是“相对于之前点再移动”,通俗一点就是说这里需要两个坐标pos1(x1,y1),pos2(x2,y2). 如果是cc.MoveTo的 ...

  10. Accessor Search Implementation Details

    [Accessor Search Implementation Details] Key-value coding attempts to use accessor methods to get an ...