有乜嘢伏? 真心請教!
function Whatever() {
this.x = -10;
this.abs = () => Math.abs(this.x);
}
let w = new Whatever();
w.abs(); // 10
lambda個this scope會跟左function個this scope
傳統function你要做呢樣野
function Whatever() {
var self = this;
this.x = -10;
this.abs = function () {
return Math.abs(self.x); // 用 this 你就食左屎
};
}
babel transpile果陣會自動幫lambda搵定個this scope
我記得有frd用lambda中過伏, 唔記得detail
但係至少由此可見lambda同function嘅sematics係唔同
如果個program冇object果類野唔洗reference this就ok
即刻寫可能1999, 有問題我再講
我可以肯肯地同你講ES6故意引入lambda去改this絕對係萬眾期待, 所有人讚不絕口既feature...
咁我都認同佢this嘅resolution係好好
我都成日用