简体   繁体   中英

Add an existing function to a class as a static method (javascript / es6)

I'd like to take an existing function

const fn = () => console.log('fn')

and add it to an existing class

class Foo { }

as a static method, so that

Foo.fn() // console prints 'fn'

Is this possible ? Something like

class Foo { static fn = fn }

throws...

You can just do something like this

 const fn = () => console.log('fn') class Foo { } Foo.fn = fn; Foo.fn() // console prints 'fn' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM