javascript实现继承的方式

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