Skip to content

Instantly share code, notes, and snippets.

@loneshark99
Created December 10, 2020 21:34
Show Gist options
  • Save loneshark99/1d19f93a2860a11c389bebeac7de1204 to your computer and use it in GitHub Desktop.
Save loneshark99/1d19f93a2860a11c389bebeac7de1204 to your computer and use it in GitHub Desktop.
Arrow Function Explanation
<!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