Skip to content

Instantly share code, notes, and snippets.

@babakness
Last active October 5, 2018 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save babakness/56da19ba85e0eaa43ae5577bc0064456 to your computer and use it in GitHub Desktop.
Save babakness/56da19ba85e0eaa43ae5577bc0064456 to your computer and use it in GitHub Desktop.
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

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