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

概要

JavaScriptでは、単一メソッドのみを定義したオブジェクトをネストさせることができます。これで制御されたメソッドチェーンを作れます。

定義

const cleanObj = (obj) => Object.assign(Object.create(null), obj);
const nestedObj = {
    a : () => cleanObj({
        b : () => cleanObj({
            c : () => console.log("I am nested!")
        })
    })
}

使用例

nestedObj.a().b().c(); 

表示

I am nested!

まとめ

JavaScriptでは、単一メソッドのみを持つオブジェクトをネストさせて、制御されたメソッドチェーンを作ることが簡単にできます。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?