Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

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

Comments

nmcr5575
@nmcr5575

これでは駄目なのでしょうか?

const x = {add: (v1, v2) => v1 + v2};
console.log(x.add(11, 21)); // 32
0

Let's comment your feelings that are more than good

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details

Being held Article posting campaign

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?

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address