≪ Today I learned.
RSS購読
    公開日
    タグ
    JavaScript
    著者
    ダーシノ

    Call-This Operatorを使ってオブジェクト指向と関数型の良いとこ取りをする

    Call-This OperatorというProposalが提案されており、これを使うとオブジェクト指向プログラミング(OOP)と関数型プログラミング(FP)の良いとこ取りができる。

    Call-This Operatorを簡単に紹介すると、Function.property.call(this, ...args)を、~>を使ってthis~>fn(...args)のように表現することができる。

    ※何年もStage 1のままで動きがないので、実際使えるようになるかはわからない。

    オブジェクト指向プログラミング(Class-based OOP)

    class Item {
      name: string
      price: number
    }
    
    class Cart {
      #items: Item = []
    
      add(item: Item): void {
        this.#items.push(item)
      }
    
      total(): number {
        return this.#items.reduce((total, a) => total += a.price, 0)
      }
    }
    
    const item = new Item('xxx', 1_000)
    const cart = new Cart()
    cart.add(item)
    const total = cart.total() // 1000

    メリット

    デメリット

    type + 関数型プログラミング

    type Item = { name: string, price: number }
    type Cart = { items: Item[] }
    
    function addItem(cart: Cart, item: Item): cart {
      return { items: [...cart.items, item] }
    }
    function getTotal(cart: Cart): number {
      return cart.items.reduce((total, a) => total += a.price, 0)
    }
    
    const item: Item = { name: 'xxx', price: 1_000 }
    const cart: Cart = { items: [] }
    
    const addedCart = addItem(cart, item)
    const total = getTotal(addedCart) // 1000

    メリット

    デメリット

    Call-This Operator(≒ Function.prototype.call)

    type Item = { name: string, price: number }
    type Cart = { items: Item[] }
    
    function addItem(this: Cart, item: Item): cart {
      return { items: [...this.items, item] }
    }
    function getTotal(this: Cart): number {
      return this.items.reduce((total, a) => total += a.price, 0)
    }
    
    const item: Item = { name: 'xxx', price: 1_000 }
    const cart: Cart = { items: [] }
    
    const addedCart = cart~>addItem(item)
    // = addItem.call(cart, item)
    const total = addedCart~>getTotal() // 1000
    // = getTotal(addedCart)

    メリット

    デメリット

    参考