避免在prototype原型函数中存在this时,使用箭头函数

function A(name){
  this.name = name; 
}
 
A.prototype.getName1 = () => {
  console.log(this.name || 'undefined'); 
}
 
A.prototype.getName2 = function() {
  console.log(this.name || 'undefined'); 
}
 
let a = new A('xx');
a.getName1();    //undefined
a.getName2();    //xx
posted @ 2019-12-12 15:53  Mr_Kahn  阅读(399)  评论(0编辑  收藏  举报