0
0

jsにおけるオブジェクトの受け取り方

Posted at

jsではオブジェクトを受け取って、受け取ったオブジェクトのキーでアクセスできます。
なので下記のように、複数のプロパティを持ったオブジェクトを渡せます。

const hoge = (obj) => {
  console.log(obj.a) // 1
  console.log(obj.b) // 2
}

hoge({a:1,b:2})

jsではキーワード引数の概念はないですが、上記のようにすることで、キーワード付きで渡すかのごとく、引数を設定できます。

Options Objectパターンというデザインパターンなんですね
https://typescriptbook.jp/reference/functions/keyword-arguments-and-options-object-pattern

// オブジェクトひとつだけを引数に持つ関数
function func(options) {
  console.log(options.x, options.y, options.z);
}
 
func({ x: 1, y: 2, z: 3 });
0
0
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
0