0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

アロー関数と通常の関数の違い(JavaScript ES6)

Last updated at Posted at 2020-05-21

#Thisの範囲の違い

  • 普通の関数
  • Thisは呼び出したオブジェクトのものになる。つまり呼び出し元のオブジェクトによってThisは変わる。
  • アロー関数
    - 宣言された関数は、宣言された時点でのThis。この場合はグローバルオブジェクトであるnum =1がThisになり決定される。呼び出し元のオブジェクトによってThisは変わることはない。
num = 1 //// JavaScriptでvarやletを付けずに宣言された変数はグローバルオブジェクトというオブジェクトのプロパティになる

const obj1 = {
 num: 444,
 fn: function() {
  console.log(this.num);
 } 
};

const obj2 = { 
 num: 888,
 fn:()=>{
  console.log(this.num);
 }
};
obj1.fn(); // 444 
obj2.fn(); // 1

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?