0
1

More than 3 years have passed since last update.

JavaScript: 引数が多いときはオブジェクトリテラルで渡すといい

Posted at

引数が多い関数、たとえば

const introduce = (name, age, from, job) => {
  console.log(`${name}さんは${age}歳、${from}出身の${job}です。`)
}

introduce('山田太郎', 20, '宮城県', 'イラストレーター');
//山田太郎さんは20歳、宮城県出身のイラストレーターです。

下のように書くといい。

const introduce = ({ name, age, from, job }) => {
  console.log(`${name}さんは${age}歳、${from}出身の${job}です。`)
}

introduce({
  name: '山田太郎',
  age: 20,
  from: '宮城県',
  job: 'イラストレーター',
});
//山田太郎さんは20歳、宮城県出身のイラストレーターです。

引数の順番を変えても問題く関数を実行できる。

introduce({
  age: 20,
  from: '宮城県',
  job: 'イラストレーター',
  name: '山田太郎',
});
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