Answers for "arrow functions vs the function keyword when writing functions in js"

0

why can't you use arrow function with this in javascript for declaring methods

'use strict';

var obj = { // does not create a new scope
  i: 10,
  b: () => console.log(this.i, this),
  c: function() {
    console.log(this.i, this);
  }
}

obj.b(); // prints undefined, Window {...} (or the global object)
obj.c()
Posted by: Guest on July-16-2021
-1

js arrow function vs function

// currently common pattern
var that = this;
getData(function(data) {
  that.data = data;
});

// better alternative with arrow functions
getData(data => {
  this.data = data;
});
Posted by: Guest on October-22-2021

Code answers related to "arrow functions vs the function keyword when writing functions in js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language