Regular: function add(a, b) { return a + b; } Arrow: const add = (a, b) => a + b;
they're both called the same way:
add(2, 3)
Regular: function greet() { return "Hi"; } Arrow: const greet = () => "Hi";
they're both called the same way:
greet()
Regular: const person = { name: "Alex", greet: function() { return "Hi, I'm " + this.name; } }; Arrow: const person = { name: "Alex", greet: () => { return "Hi, I'm " + this.name; } };
when using the arrow method, 'this' is not a name. It's whatever 'this' was outside the object you defined with the arrow --usually a global object (window in browsers). Arrow functions (functions defined using arrows) never change 'this.'
this
)new
with them)this
context is needed