箭头函数的this值
<p><strong>箭头函数不绑定this,会捕获其所在的上下文的this值,作为自己的this值</strong></p>
<pre><code class="language-javascript">var obj = {
a: 10,
b: () => {
console.log(this.a); // undefined
console.log(this); // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
},
c: function() {
console.log(this.a); // 10
console.log(this); // {a: 10, b: ƒ, c: ƒ}
}
}
obj.b();
obj.c();</code></pre>
<pre><code class="language-javascript">var obj = {
a: 10,
b: function(){
console.log(this.a); //10
},
c: function() {
return ()=>{
console.log(this.a); //10
}
}
}
obj.b();
obj.c()();</code></pre>