#Object literal that has two functions that share an implementation?

7 messages · Page 1 of 1 (latest)

split dune
#

See the screenshot for context. Basically I want to figure out how to achieve this in ts without type assertions and without assigning the shared implementation a name in the outer scope:

const x = {a: (() => "a")};
x.b = x.a;
x.b();
-> "a"
tidal thunder
#
const x = {
    a: () => 'a',

    b() {
        return this.a()
    },
}
#

Not sure why you couldn't just do:

const fn = () => 'a'
const x = { a: fn, b: fn }
split dune
tidal thunder
#

Eh, I mean if you want to reference something twice, that something has to have a name.

#

Whether that name is fn or this.a.

split dune
#

thanks @tidal thunder