39
39

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 5 years have passed since last update.

JavaScript ES6で関数の戻り値が複数の場合のスッキリした実装

Posted at

関数の戻り値が複数の場合のスッキリした実装。

実装

関数の戻り値をオブジェクトにすると、複数の戻り値を返すことができる

function func(){
  let x = 1;
  let y = 2;
  return {x: x, y: y}
}

オブジェクトでキーと値が同じ場合は省略できる

let x = 1;
let y = 2;
let xy = {x, y};

オブジェクトを分割代入すると楽

let xy = {x: 1, y: 2};
let {x, y} = xy;

これらを合わせると

function func(){
  let x = 1;
  let y = 2;
  return {x, y}
}

let {x, y} = func();

スッキリ

39
39
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
39
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?