Forward Thinking — a never-complete guide to modern JavaScript #2 Arrow Function
I think everybody has seen many „=>” in JavaScript these days. The Arrow Function itself. In many cases it solves problems with „this”. It’s shorter. I really like it.
But we need to remember that Arrow Functions are a little different from traditional JavaScript functions — that’s what makes them special and useful for us.
Let’s examine differences which you can stumble upon:
- Cannot be used as constructors
This code will not work:
const Constructor = () => {
console.log("Mr. Watson, Come Here");
};
let myObject = new Constructor();
Try it by yourself.
It’s because Arrow Functions do not have Construct method.
2. No prototype
Because we cannot use new on them there is no use/need for prototype either.
3. No Arguments object inside
This code will not work:
const myFunc = () => {
console.log(arguments);
};
myFunc("myFirstArg", "my2ndArg");