1. 程式人生 > >es6 箭頭函數 this 問題

es6 箭頭函數 this 問題

之前 target round size clas body -s targe win

1. 在箭頭函數出現之前,每個新定義的函數都有其自己的this值(例如,構造函數的 this 指向了一個新的對象;嚴格模式下的函數的 this 值為 undefined;如果函數是作為對象的方法被調用的,則其 this 指向了那個調用它的對象)。

2. 箭頭函數沒有自己的this,不會新產生自己作用域下的this,arguments,super和new.target等對象。此外,箭頭函數總是匿名的。

<input type="button" class="btn1" value="提交1">
<input type="button" class="btn2" value
="提交2"> <script> $(".btn1").click(function () { console.log(1); console.log(this);// <input type="button" class="btn1" value="提交1"> }) $(".btn2").click(() => { console.log(2); console.log(this);// window }) </script>

es6 箭頭函數 this 問題