|
| 1 | +//在绝大多数情况下,函数的调用方式决定了this的值。this不能在执行期间被赋值,在每次函数被调用时this的值也可能会不同。ES5引入了bind方法来设置函数的this值,而不用考虑函数如何被调用的 |
| 2 | + |
| 3 | +//全局上下文的this |
| 4 | + |
| 5 | +console.log(this.document === document); // true |
| 6 | + |
| 7 | +// 在浏览器中,全局对象为 window 对象: |
| 8 | +console.log(this === window); // true |
| 9 | + |
| 10 | +this.a = 37; |
| 11 | +console.log(window.a); // 37 |
| 12 | + |
| 13 | +//函数上下文的this |
| 14 | + |
| 15 | +//直接调用 |
| 16 | +function f1(){ |
| 17 | + return this; |
| 18 | +} |
| 19 | + |
| 20 | +f1() === window; // true |
| 21 | +//在上面的例子中,this的值不是由函数调用设定。因为代码不是在严格模式下执行,this 的值总是一个对象且默认为全局对象。 |
| 22 | + |
| 23 | +function f2(){ |
| 24 | + "use strict"; // 这里是严格模式 |
| 25 | + return this; |
| 26 | +} |
| 27 | +//在严格模式下,this 是在进入运行环境时设置的。若没有定义,this的值将维持undefined状态。同时它也能设置成任意值,比如 null 或者42 或者“I am not this”。 |
| 28 | +f2() === undefined; // true |
| 29 | + |
| 30 | +//对象方法中的this |
| 31 | +//当以对象里的方法的方式调用函数时,它们的 this 是调用该函数的对象. |
| 32 | +var o = { |
| 33 | + prop: 37, |
| 34 | + f: function() { |
| 35 | + return this.prop; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +console.log(o.f()); // logs 37 |
| 40 | + |
| 41 | +//注意,在何处或者如何定义调用函数完全不会影响到this的行为 |
| 42 | +var o = {prop: 37}; |
| 43 | + |
| 44 | +function independent() { |
| 45 | + return this.prop; |
| 46 | +} |
| 47 | + |
| 48 | +o.f = independent; |
| 49 | + |
| 50 | +console.log(o.f()); // logs 37 |
| 51 | + |
| 52 | +//类似的,this 的绑定只受最靠近的成员引用的影响。在下面的这个例子中,我们把一个方法g当作对象o.b的函数调用。在这次执行期间,函数中的this将指向o.b。事实上,这与对象本身的成员没有多大关系,最靠近的引用才是最重要的 |
| 53 | +o.b = { |
| 54 | + g: independent, |
| 55 | + prop: 42 |
| 56 | +}; |
| 57 | +console.log(o.b.g()); // logs 42 |
| 58 | + |
| 59 | +//原型链中的this |
| 60 | +//如果该方法存在于一个对象的原型链上,那么this指向的是调用这个方法的对象,表现得好像是这个方法就存在于这个对象上一样 |
| 61 | + |
| 62 | +var o = { |
| 63 | + f : function(){ |
| 64 | + return this.a + this.b; |
| 65 | + } |
| 66 | +}; |
| 67 | +var p = Object.create(o); |
| 68 | +p.a = 1; |
| 69 | +p.b = 4; |
| 70 | + |
| 71 | +console.log(p.f()); // 5 |
| 72 | + |
| 73 | +//在这个例子中,对象p没有属于它自己的f属性,它的f属性继承自它的原型。但是这对于最终在o中找到f属性的查找过程来说没有关系;查找过程首先从p.f的引用开始,所以函数中的this指向p。也就是说,因为f是作为p的方法调用的,所以它的this指向了p。这是JavaScript的原型继承中的一个有趣的特性 |
| 74 | + |
| 75 | + |
| 76 | +//getter 与 setter 中的 this |
| 77 | +//再次,相同的概念也适用时的函数作为一个 getter 或者 一个setter调用。作为getter或setter函数都会绑定 this 到从设置属性或得到属性的那个对象 |
| 78 | + |
| 79 | +function modulus(){ |
| 80 | + return Math.sqrt(this.re * this.re + this.im * this.im); |
| 81 | +} |
| 82 | + |
| 83 | +var o = { |
| 84 | + re: 1, |
| 85 | + im: -1, |
| 86 | + get phase(){ |
| 87 | + return Math.atan2(this.im, this.re); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +Object.defineProperty(o, 'modulus', { |
| 92 | + get: modulus, enumerable:true, configurable:true}); |
| 93 | + |
| 94 | +console.log(o.phase, o.modulus); // logs -0.78 1.4142 |
| 95 | + |
| 96 | + |
| 97 | +//构造函数中的 this |
| 98 | +//当一个函数被作为一个构造函数来使用(使用new关键字),它的this与即将被创建的新对象绑定 |
| 99 | +//注意:当构造器返回的默认值是一个this引用的对象时,可以手动设置返回其他的对象,如果返回值不是一个对象,返回this |
| 100 | + |
| 101 | +function C(){ |
| 102 | + this.a = 37; |
| 103 | +} |
| 104 | + |
| 105 | +var o = new C(); |
| 106 | +console.log(o.a); // logs 37 |
| 107 | + |
| 108 | + |
| 109 | +function C2(){ |
| 110 | + this.a = 37; |
| 111 | + return {a:38}; |
| 112 | +} |
| 113 | + |
| 114 | +o = new C2(); |
| 115 | +console.log(o.a); // logs 38 |
| 116 | + |
| 117 | +//在最后的例子中(C2),因为在调用构造函数的过程中,手动的设置了返回对象,与this绑定的默认对象被取消(本质上这使得语句“this.a = 37;”成了“僵尸”代码,实际上并不是真正的“僵尸”,这条语句执行了但是对于外部没有任何影响,因此完全可以忽略它) |
| 118 | + |
| 119 | + |
| 120 | +//call 和 apply |
| 121 | +//当一个函数的函数体中使用了this关键字时,通过所有函数都从Function对象的原型中继承的call()方法和apply()方法调用时,它的值可以绑定到一个指定的对象上 |
| 122 | + |
| 123 | +function add(c, d){ |
| 124 | + return this.a + this.b + c + d; |
| 125 | +} |
| 126 | + |
| 127 | +var o = {a:1, b:3}; |
| 128 | + |
| 129 | +// The first parameter is the object to use as 'this', subsequent parameters are passed as |
| 130 | +// arguments in the function call |
| 131 | +add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16 |
| 132 | + |
| 133 | +// The first parameter is the object to use as 'this', the second is an array whose |
| 134 | +// members are used as the arguments in the function call |
| 135 | +add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34 |
| 136 | + |
| 137 | +//使用 call 和 apply 函数的时候要注意,如果传递的 this 值不是一个对象,JavaScript 将会尝试使用内部 ToObject 操作将其转换为对象。因此,如果传递的值是一个原始值比如 7 或 'foo' ,那么就会使用相关构造函数将它转换为对象,所以原始值 7 通过new Number(7)被转换为对象,而字符串'foo'使用 new String('foo') 转化为对象,例如 |
| 138 | +function bar() { |
| 139 | + console.log(Object.prototype.toString.call(this)); |
| 140 | +} |
| 141 | + |
| 142 | +bar.call(7); // [object Number] |
| 143 | + |
| 144 | + |
| 145 | +//bind方法中的this |
| 146 | +//ECMAScript 5 引入了 Function.prototype.bind。调用f.bind(someObject)会创建一个与f具有相同函数体和作用域的函数,但是在这个新函数中,this将永久地被绑定到了bind的第一个参数,无论这个函数是如何被调用的 |
| 147 | + |
| 148 | +function f(){ |
| 149 | + return this.a; |
| 150 | +} |
| 151 | + |
| 152 | +var g = f.bind({a:"azerty"}); |
| 153 | +console.log(g()); // azerty |
| 154 | + |
| 155 | +var o = {a:37, f:f, g:g}; |
| 156 | +console.log(o.f(), o.g()); // 37, azerty |
| 157 | + |
| 158 | +//DOM事件处理函数中的 this |
| 159 | + |
| 160 | +//当函数被用作事件处理函数时,它的this指向触发事件的元素(一些浏览器在动态添加监听器时不遵守这个约定,除非使用addEventListener 这句不太确定翻译的是否正确) |
| 161 | + |
| 162 | +// 被调用时,将关联的元素变成蓝色 |
| 163 | +function bluify(e){ |
| 164 | + console.log(this === e.currentTarget); // 总是 true |
| 165 | + |
| 166 | + // 当 currentTarget 和 target 是同一个对象是为 true |
| 167 | + console.log(this === e.target); |
| 168 | + this.style.backgroundColor = '#A5D9F3'; |
| 169 | +} |
| 170 | + |
| 171 | +// 获取文档中的所有元素的列表 |
| 172 | +var elements = document.getElementsByTagName('*'); |
| 173 | + |
| 174 | +// 将bluify作为元素的点击监听函数,当元素被点击时,就会变成蓝色 |
| 175 | +for(var i=0 ; i<elements.length ; i++){ |
| 176 | + elements[i].addEventListener('click', bluify, false); |
| 177 | +} |
| 178 | + |
| 179 | + |
| 180 | +//内联事件处理函数中的 this |
| 181 | + |
| 182 | +//当代码被内联处理函数调用时,它的this指向监听器所在的DOM元素 |
| 183 | + |
| 184 | +<button onclick="alert(this.tagName.toLowerCase());"> |
| 185 | + Show this |
| 186 | +</button> |
| 187 | + |
| 188 | +//上面的alert会显示button。注意只有外层代码中的this是这样设置的 |
| 189 | +<button onclick="alert((function(){return this})());"> |
| 190 | + Show inner this |
| 191 | +</button> |
| 192 | +//在这种情况下,没有设置内部函数的 this,所以它指向 global/window 对象(即非严格模式下调用的函数未设置 this 时指向的默认对象) |
| 193 | + |
0 commit comments