Besides Syntax, Is There Any Difference Between A Normal Function And An Arrow Function?
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 def
Solution 1:
One thing to note is that an arrow function does not have use of the arguments
object.
lettest = () => {
// expect an errorconsole.log(arguments);
}
test(1,2,3);
- Arrow functions lexically bind
this
- You cannot use
new
on an arrow function:
letPerson = (name) => {
this.name = name;
}
// expect an errorlet person = newPerson('Bob');
There are many differences, I would check some of the documentation on arrow functions.
Solution 2:
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).
Post a Comment for "Besides Syntax, Is There Any Difference Between A Normal Function And An Arrow Function?"