Last active
February 22, 2021 13:13
-
-
Save sayinserdar/e6b6594af629415b75dbd14d11c9aafc to your computer and use it in GitHub Desktop.
Arrow functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example is from = https://www.javascripttutorial.net/es6/javascript-arrow-function/ | |
// Normal/Traditional function | |
function Car() { | |
this.speed = 0; | |
this.speedUp = function (speed) { | |
this.speed = speed; | |
let self = this; | |
// Creates it's own scope. You have to create another variable to access the upper scope | |
setTimeout(function () { | |
console.log(self.speed); | |
}, 1000); | |
}; | |
} | |
let car = new Car(); | |
car.speedUp(50); // 50; | |
// Arrow function | |
function Car() { | |
this.speed = 0; | |
this.speedUp = function (speed) { | |
this.speed = speed; | |
// No need to create a new variable. You can directly access the variable | |
setTimeout( | |
() => console.log(this.speed), | |
1000); | |
}; | |
} | |
let car = new Car(); | |
car.speedUp(50); // 50; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment