Use javascript's default parameters to create an init function

ES6 has default parameters.

var country = ( name = "Zimbabwe" ) => {
  return `You are in ${name}`


That will return You are in Ghana and You are in Zimbabwe respectively.

You can use this to ensure one function is called before another.

Use a final function parameter that is never passed anything. We give this final parameter a default value. And that default value is a function call.

var init_function = _ => {
  console.log("Ooh, a function use...")
var country = ( name, _run_me = init_function() ) =>
  `You are in ${name}`


This returns

Oooh, a function use...
You are in Botswana

Why, oh why do we want to do that? We could just use the first line of our function. But, for syntactic reasons, we may not want to use curly brackets and a return statement--we may want a one-liner--and this gives us the ability to have that while specifying a function to run beforehand.


Edit on github
comments powered by Disqus
Click me