Skip to content

Instantly share code, notes, and snippets.

@olpoco
Last active March 14, 2016 01:31
Show Gist options
  • Save olpoco/e6bca29f9403eb8f2fc4 to your computer and use it in GitHub Desktop.
Save olpoco/e6bca29f9403eb8f2fc4 to your computer and use it in GitHub Desktop.
js partial function with bind

Awesome trick seen on mongoose doc:

db.on('error', console.error.bind(console, 'connection error:'));

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

so the above is equivalent to console.log with first argument set to 'connection error:', so when errors it prints something like

connection error: { [MongoError: getaddrinfo EIO] name: 'MongoError', message: 'getaddrinfo EIO' }

more tricks using bind

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