0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptで単一メソッドのオブジェクトを作成する関数

Posted at

この記事では、単一のメソッドのみを持つオブジェクトを動的に作成する関数monoMethodObjを紹介します。
この関数を使うと、関数名と処理内容を指定するだけで、簡潔なオブジェクトを生成できます。

定義

const monoMethodObj = (name = "_", fnc = v => v) => ({ [name]: fnc });

使用例

const x = monoMethodObj("add", (v1, v2) => v1 + v2);
console.log(x.add(11, 21));

表示結果

32

まとめ

monoMethodObjを使うことで、単一のメソッドのみを持つオブジェクトを動的に作成して活用できます。

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?