2017-09-22 80 views
2

下面的代码不起作用:为什么我不能使用ES6函数语法作为构造函数?

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功能和常规功能之间的区别?

+3

由于箭头函数并非旨在用作构造函数,它的目的也不在于用作方法。阅读[documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)总是有用的。 – Teemu

回答

2

箭头函数不处理this关键字与正常函数的关系。

内部箭头功能this值是一样的是什么功能外

相关问题