以下代码不起作用:
let myClass = ()=>{
this.value = 2
return this
}
myClass.prototype.print = ()=>{
console.log(this.value)
}
虽然这有效
let myClass = function(){
this.value = 2
return this
}
myClass.prototype.print = function(){
console.log(this.value)
}
ES6函数和普通函数有什么区别?
最佳答案
箭头函数不会像处理普通函数那样处理 this
关键字。
在箭头函数内部,this
的值与函数外部的值相同
关于javascript - 为什么我不能使用 ES6 函数语法作为构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46358986/