...
this
binding:Regular: Has its own
this
contextArrow: Inherits
this
from the enclosing scopeCode Block language js const obj = { name: 'pfx', sayHiRegular: function() { console.log('Hi, ' + this.name); }, sayHiArrow: () => { console.log('Hi, ' + this.name); } }; obj.sayHiRegular(); // Outputs: Hi, pfx obj.sayHiArrow(); // Outputs: Hi, undefined
Arguments object:
Regular: Has
arguments
objectArrow: Doesn't have
arguments
objectCode Block language js function regularFunc() { console.log(arguments); } const arrowFunc = () => { console.log(arguments); }; regularFunc(1, 2, 3); // Outputs: [1, 2, 3] arrowFunc(1, 2, 3); // Throws ReferenceError: arguments is not defined
Use as methodsin objects:
Regular: Can be used as object methodsuse values from object using ‘this’ keyword
Arrow: Not suitable for object methods (due Can’t react values from the object as don’t have access to 'this' binding)
Code Block language js function regularFunc()const regularObject = { name: 'Regular', console.log(arguments); } const arrowFunc = (greet: function() { console.log(`Hello, ${this.name}!`); } }; const arrowObject = { name: 'Arrow', greet: () => { console.log(arguments`Hello, ${this.name}!`); } }; regularFunc(1, 2, 3regularObject.greet(); // OutputsOutput: [1Hello, 2, 3] arrowFunc(1, 2, 3); Regular! arrowObject.greet(); // Throws ReferenceErrorOutput: arguments is not definedHello, undefined!
Function hoisting:
Regular: Can be hoisted
Arrow: Cannot be hoisted
Code Block language js console.log(regularHoisted()); // Outputs: "I'm hoisted!" function regularHoisted() { return "I'm hoisted!"; } // Uncommenting the next line would throw a ReferenceError console.log(arrowHoisted()); const arrowHoisted = () => "I'm not hoisted!";
Constructors:
Regular: Can be used as constructors
Arrow: Cannot be used as constructors
Code Block language js function RegularConstructor() { this.value = 42; } const regularInstance = new RegularConstructor(); console.log(regularInstance.value); // Outputs: 42 const ArrowConstructor = () => { this.value = 42; }; // Uncommenting the next line would throw a TypeError // const arrowInstance = new ArrowConstructor();
...