Arrow Functions Caveats

// no arrow function (works as expected)
var Cookie1 = function Cookie() {
this.type = 'chocolate-chip';
}
var cookie1 = new Cookie1();
Cookie1.prototype.hi = function() {
console.log(this);
}
cookie1.hi(); // => Cookie {'type': 'chocolate-chip'}
// arrow function on the Constructor (doesnt work)
var Cookie2 = () => {
this.type = 'chocolate-chip';
}
var cookie2 = new Cookie2();
Cookie2.prototype.hi = function() {
console.log(this);
}
cookie2.hi(); // => Uncaught TypeError: Cookie3 is not a constructor
// with arrow function on the prototype (it doesn't work)
var Cookie3 = function Cookie() {
this.type = 'chocolate-chip';
}
var cookie3 = new Cookie3();
Cookie3.prototype.hi = () => {
console.log(this);
}
cookie3.hi(); // => Window or undefined in 'use strict' mode
Show your support

Clapping shows how much you appreciated David Kang’s story.