アロー関数の中と外のthisは同じ

ただの関数ではなくアロー関数内でのthisは、アロー関数を囲むコンテキストの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>
    

    Node.jsでアロー関数内のthis

    #!/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モード()
    関数式ゆるいモード()