Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helpers for creating class like functions that can be invoked without new
const assoc = ( prop, value, obj ) =>
Object.assign( {}, obj, { [prop]: value })
const reducer = ( $values, accumulate, [key,val] ) => assoc( key, val.bind( undefined, ...$values ), accumulate )
export const bindValuesToMethods = ( $methods, ...$values ) =>
Object.entries( $methods ).reduce( reducer.bind( undefined, ...$values ), {} )
export const prepareInstance = (instanceMethods, staticMethods = ({}) ) => Object.assign(
bindValuesToMethods.bind( undefined, instanceMethods ),
staticMethods
)
@babakness
Copy link
Author

babakness commented Feb 21, 2018

New name suggestions for "prepareInstance" welcomed!

@pre1ude
Copy link

pre1ude commented Oct 5, 2018

export const bindValuesToMethods = ( $methods, ...$values ) => 
  Object.entries( $methods ).reduce( reducer.bind( undefined, ...$values ), {} )

should be

export const bindValuesToMethods = ($methods, ...$values) =>
  Object.entries($methods).reduce(reducer.bind(undefined, $values), {})

else error will happed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment