Created
December 10, 2020 21:34
-
-
Save loneshark99/1d19f93a2860a11c389bebeac7de1204 to your computer and use it in GitHub Desktop.
Arrow Function Explanation
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script> | |
// Traditional functions default this to the window scope: | |
window.age = 10; | |
function Person() { | |
this.age = 42; | |
setTimeout(function () { | |
console.log("this.age", this.age) | |
}, 1000); | |
} | |
var p = new Person() | |
// Arrow functions do not default this to the window scope, rather they execute in the scope they are created: | |
window.Aage = 10; | |
function APerson() { | |
this.Aage = 42; | |
setTimeout(() => { | |
console.log("this.Aage", this.Aage) | |
}, 2000); | |
} | |
var p1 = APerson() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment