2

The following code doesn't work:

let myClass = ()=>{
    this.value = 2
    return this
}

myClass.prototype.print = ()=>{
    console.log(this.value)
}

While this works

let myClass = function(){
    this.value = 2
    return this
}

myClass.prototype.print = function(){
    console.log(this.value)
}

What was the difference between ES6 function and regular function?

James Maa
  • 1,764
  • 10
  • 20
  • 3
    Because arrow function is not purposed to be used as a constructor, neither it is purposed to be used as a method. Reading the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) is always useful. – Teemu Sep 22 '17 at 07:21

2 Answers2

2

Arrow functions does not deal with this keyword the way it does with normal functions.

Inside arrow functions the value of this is same as what it is outside the function

Rohit Agrawal
  • 1,496
  • 9
  • 20
2

Because arrow functions do not have a prototype property.

dork
  • 4,396
  • 2
  • 28
  • 56