ただの関数ではなくアロー関数内でのthis
は、アロー関数を囲むコンテキストのthis
と同じものを指す。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>アロー関数内のthis</title>
<script>
// アロー関数内のthisはアロー関数を囲むコンテキストにおけるthisと同じ。
console.log(this === window) // true (モードによらず)
let アロー関数 = () => {
console.log(this === window) // true (上と同じ)
}
let 関数式strictモード = function() {
'use strict'
console.log(this === undefined) // true
let アロー関数 = () => {
console.log(this === undefined) // true (上と同じ)
}
アロー関数()
}
let 関数式ゆるいモード = function() {
console.log(this === window) // true
let アロー関数 = () => {
console.log(this === window) // true (上と同じ)
}
アロー関数()
}
アロー関数()
関数式strictモード()
関数式ゆるいモード()
</script>
</head>
<body>
</body>
</html>
#!/usr/bin/env node
// アロー関数内のthisはアロー関数を囲むコンテキストにおけるthisと同じ。
console.log(this === module.exports) // true (モードによらず)
let アロー関数 = () => {
console.log(this === module.exports) // true (上と同じ)
}
let 関数式strictモード = function() {
'use strict'
console.log(this === undefined) // true
let アロー関数 = () => {
console.log(this === undefined) // true (上と同じ)
}
アロー関数()
}
let 関数式ゆるいモード = function() {
console.log(this === global) // true
let アロー関数 = () => {
console.log(this === global) // true (上と同じ)
}
アロー関数()
}
アロー関数()
関数式strictモード()
関数式ゆるいモード()