Skip to content

Instantly share code, notes, and snippets.

@sayinserdar
Last active February 22, 2021 13:13
Show Gist options
  • Save sayinserdar/e6b6594af629415b75dbd14d11c9aafc to your computer and use it in GitHub Desktop.
Save sayinserdar/e6b6594af629415b75dbd14d11c9aafc to your computer and use it in GitHub Desktop.
Arrow functions
// 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