这一章咱们将会重点介绍JavaScript中几个重要的属性(this、constructor、prototype), 这些属性对于咱们理解如何实现JavaScript中的类和继承起着相当重要的做用。javascript
this表示当前对象,若是在全局做用范围内使用this,则指代当前页面对象window; 若是在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用。 咱们还可使用apply和call两个全局方法来改变函数中this的具体指向。java
先看一个在全局做用范围内使用this的例子:数组
1 <script type="text/javascript"> 2 console.log(this === window); // true 3 console.log(window.alert === this.alert); // true 4 console.log(this.parseInt("021", 10)); // 10 5 </script>
函数中的this是在运行时决定的,而不是函数定义时,以下:app
1 // 定义一个全局函数 2 function foo() { 3 console.log(this.fruit); 4 } 5 // 定义一个全局变量,等价于window.fruit = "apple"; 6 var fruit = "apple"; 7 // 此时函数foo中this指向window对象 8 // 这种调用方式和window.foo();是彻底等价的 9 foo(); // "apple" 10 11 // 自定义一个对象,并将此对象的属性foo指向全局函数foo 12 var pack = { 13 fruit: "orange", 14 foo: foo 15 }; 16 // 此时函数foo中this指向window.pack对象 17 pack.foo(); // "orange"
全局函数apply和call能够用来改变函数中this的指向,以下:函数
1 // 定义一个全局函数 2 function foo() { 3 console.log(this.fruit); 4 } 5 6 // 定义一个全局变量 7 var fruit = "apple"; 8 // 自定义一个对象 9 var pack = { 10 fruit: "orange" 11 }; 12 13 // 等价于window.foo(); 14 foo.apply(window); // "apple" 15 // 此时foo中的this === pack 16 foo.apply(pack); // "orange"
注:apply和call两个函数的做用相同,惟一的区别是两个函数的参数定义不一样。ui
由于在JavaScript中函数也是对象,因此咱们能够看到以下有趣的例子:this
1 // 定义一个全局函数 2 function foo() { 3 if (this === window) { 4 console.log("this is window."); 5 } 6 } 7 8 // 函数foo也是对象,因此能够定义foo的属性boo为一个函数 9 foo.boo = function() { 10 if (this === foo) { 11 console.log("this is foo."); 12 } else if (this === window) { 13 console.log("this is window."); 14 } 15 }; 16 // 等价于window.foo(); 17 foo(); // this is window. 18 19 // 能够看到函数中this的指向调用函数的对象 20 foo.boo(); // this is foo. 21 22 // 使用apply改变函数中this的指向 23 foo.boo.apply(window); // this is window.
咱们已经在第一章中使用prototype模拟类和继承的实现。 prototype本质上仍是一个JavaScript对象。 而且每一个函数都有一个默认的prototype属性。
若是这个函数被用在建立自定义对象的场景中,咱们称这个函数为构造函数。 好比下面一个简单的场景:spa
1 // 构造函数 2 function Person(name) { 3 this.name = name; 4 } 5 // 定义Person的原型,原型中的属性能够被自定义对象引用 6 Person.prototype = { 7 getName: function() { 8 return this.name; 9 } 10 } 11 var zhang = new Person("ZhangSan"); 12 console.log(zhang.getName()); // "ZhangSan" 13
做为类比,咱们考虑下JavaScript中的数据类型 - 字符串(String)、数字(Number)、数组(Array)、对象(Object)、日期(Date)等。 咱们有理由相信,在JavaScript内部这些类型都是做为构造函数来实现的,好比:prototype
1 // 定义数组的构造函数,做为JavaScript的一种预约义类型 2 function Array() { 3 // ... 4 } 5 6 // 初始化数组的实例 7 var arr1 = new Array(1, 56, 34, 12); 8 // 可是,咱们更倾向于以下的语法定义: 9 var arr2 = [1, 56, 34, 12]; 10
同时对数组操做的不少方法(好比concat、join、push)应该也是在prototype属性中定义的。
实际上,JavaScript全部的固有数据类型都具备只读的prototype属性(这是能够理解的:由于若是修改了这些类型的prototype属性,则哪些预约义的方法就消失了), 可是咱们能够向其中添加本身的扩展方法。code
1 // 向JavaScript固有类型Array扩展一个获取最小值的方法 2 Array.prototype.min = function() { 3 var min = this[0]; 4 for (var i = 1; i < this.length; i++) { 5 if (this[i] < min) { 6 min = this[i]; 7 } 8 } 9 return min; 10 }; 11 12 // 在任意Array的实例上调用min方法 13 console.log([1, 56, 34, 12].min()); // 1 14
注意:这里有一个陷阱,向Array的原型中添加扩展方法后,当使用for-in循环数组时,这个扩展方法也会被循环出来。
下面的代码说明这一点(假设已经向Array的原型中扩展了min方法):
1 var arr = [1, 56, 34, 12]; 2 var total = 0; 3 for (var i in arr) { 4 total += parseInt(arr[i], 10); 5 } 6 console.log(total); // NaN 7 8
解决方法也很简单:
1 var arr = [1, 56, 34, 12]; 2 var total = 0; 3 for (var i in arr) { 4 if (arr.hasOwnProperty(i)) { 5 total += parseInt(arr[i], 10); 6 } 7 } 8 console.log(total); // 103 9
constructor始终指向建立当前对象的构造函数。好比下面例子:
1 // 等价于 var foo = new Array(1, 56, 34, 12); 2 var arr = [1, 56, 34, 12]; 3 console.log(arr.constructor === Array); // true 4 // 等价于 var foo = new Function(); 5 var Foo = function() { }; 6 console.log(Foo.constructor === Function); // true 7 // 由构造函数实例化一个obj对象 8 var obj = new Foo(); 9 console.log(obj.constructor === Foo); // true 10 11 // 将上面两段代码合起来,就获得下面的结论 12 console.log(obj.constructor.constructor === Function); // true 13
可是当constructor遇到prototype时,有趣的事情就发生了。
咱们知道每一个函数都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数。以下例所示:
1 function Person(name) { 2 this.name = name; 3 }; 4 Person.prototype.getName = function() { 5 return this.name; 6 }; 7 var p = new Person("ZhangSan"); 8 9 console.log(p.constructor === Person); // true 10 console.log(Person.prototype.constructor === Person); // true 11 // 将上两行代码合并就获得以下结果 12 console.log(p.constructor.prototype.constructor === Person); // true
当时当咱们从新定义函数的prototype时(注意:和上例的区别,这里不是修改而是覆盖), constructor的行为就有点奇怪了,以下示例:
1 function Person(name) { 2 this.name = name; 3 }; 4 Person.prototype = { 5 getName: function() { 6 return this.name; 7 } 8 }; 9 var p = new Person("ZhangSan"); 10 console.log(p.constructor === Person); // false 11 console.log(Person.prototype.constructor === Person); // false 12 console.log(p.constructor.prototype.constructor === Person); // false
为何呢?
原来是由于覆盖Person.prototype时,等价于进行以下代码操做:
1 Person.prototype = new Object({ 2 getName: function() { 3 return this.name; 4 } 5 }); 6
而constructor始终指向建立自身的构造函数,因此此时Person.prototype.constructor === Object,便是:
1 function Person(name) { 2 this.name = name; 3 }; 4 Person.prototype = { 5 getName: function() { 6 return this.name; 7 } 8 }; 9 var p = new Person("ZhangSan"); 10 console.log(p.constructor === Object); // true 11 console.log(Person.prototype.constructor === Object); // true 12 console.log(p.constructor.prototype.constructor === Object); // true
怎么修正这种问题呢?方法也很简单,从新覆盖Person.prototype.constructor便可:
1 function Person(name) { 2 this.name = name; 3 }; 4 Person.prototype = new Object({ 5 getName: function() { 6 return this.name; 7 } 8 }); 9 Person.prototype.constructor = Person; 10 var p = new Person("ZhangSan"); 11 console.log(p.constructor === Person); // true 12 console.log(Person.prototype.constructor === Person); // true 13 console.log(p.constructor.prototype.constructor === Person); // true 14