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・jQueryでES6のアロー関数を使ったときの引数について

Last updated at Posted at 2021-05-06

アロー関数を使ったときによく引数を忘れるため、自分用のメモとして記事にしました。

##forEachメソッドの引数
###配列の場合

javascript
const array1 = ['a', 'b', 'c'];
array1.forEach((element) => { 
console.log(element);
});
//結果
// "a"
// "b"
// "c"

###連想配列の場合

javascript
const hoge = ['key0':'value0','key1':'value1','key2':'value2'];
Object.keys(hoge).forEach((key)=>{
 console.log(key);
})
//結果
// "key0"
// "key1"
// "key2"

##eachメソッドの引数

html
<ul>
 <li>テキスト1</li>
 <li>テキスト2</li>
 <li>テキスト3</li>
</ul>
jQuery
$('ul li').each((index, element) => {
  console.log(index); // 0~2の数字
  console.log(element); // liのエレメント情報
});

##addEventListener

html
<button>ボタン</button>
jQuery
document.querySelector('button').addEventListener('click',(event) => {
  console.log(event.currentTarget); // buttonのエレメント情報
});

##onメソッド

html
<button>ボタン</button>
jQuery
$('button').on('click',(event) => {
  console.log(event.currentTarget); // buttonのエレメント情報
});
0
1
0

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?