0

I've recently begun using ECMAScript 2016's arrow functions instead of the original syntax to define functions. Would it be ok if I just used this syntax every time I wanted to define a function, or are are there any disadvantages like speed, etc.?

(function(){
  alert('Is this');
})();

(()=>{
  alert('somehow better than this?')
})();
feihcsim
  • 1,138
  • 2
  • 14
  • 25

2 Answers2

4

One thing to note is that an arrow function does not have use of the arguments object.

let test = () => {
  // expect an error
  console.log(arguments);
}

test(1,2,3);
  • Arrow functions lexically bind this
  • You cannot use new on an arrow function:

let Person = (name) => {
  this.name = name;
}
   
// expect an error
let person = new Person('Bob');

There are many differences, I would check some of the documentation on arrow functions.

KevBot
  • 14,556
  • 3
  • 37
  • 59
  • I also noticed that firefox uses 2-3x more time with arrow functions, but i just did a quick perf test. – bjanes Jul 22 '16 at 23:04
1

Arrow functions are always anonymous and have lexical this. Differences in performance should be negligible but this might refer to something unexpected (or maybe it refers to exactly what you expect and you won't have to bind).