この記事では、単一のメソッドのみを持つオブジェクトを動的に作成する関数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を使うことで、単一のメソッドのみを持つオブジェクトを動的に作成して活用できます。