Earlier function parameters are available to default parameters (#tilPost)

Here’s Alex MacArthur with a nice JavaScript fun fact: when using JavaScript default parameters, earlier parameters are available to later default parameters.
Let’s look at some code from his post because the previous sentence is a …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Here's Alex MacArthur with a nice JavaScript fun fact: when using JavaScript default parameters, earlier parameters are available to later default parameters.

Let's look at some code from his post because the previous sentence is a mouthful.

// reuse `arg1` and set it as `arg2`
function myFunc(arg1, arg2 = arg1) {
  console.log(arg1, arg2);
}

myFunc("arg1!"); // "arg1!" "arg1!"

That's pretty cool, but when would I use this?

Alex describes using this behavior to make a JavaScript class more testable. Fair, but I rarely use classes paired with dependency injection, so this example isn't convincing for me.

He also came up with the least readable reduce function at all times.

const numbers = [1, 2, 3];

// don't do this 😅
const total = numbers.reduce(
  (acc, value, _index, _array, result = acc + value) => result
);

But the post's comments include some gems.

// Make an image a square if not defined otherwise 
// Kudos to Colum
function Image(width, height = width) {}

// Find a string with defined boundaries
// if no boundaries are defined, search the entire string
// Kudos to Axel Rauschmayer
function findIn(str, start = 0, end = str.length) {}

Great examples! I'm convinced and will put reusable function parameters into my JavaScript toolbelt.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2024-11-03T19:00:00+00:00) Earlier function parameters are available to default parameters (#tilPost). Retrieved from https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/

MLA
" » Earlier function parameters are available to default parameters (#tilPost)." Stefan Judis | Sciencx - Sunday November 3, 2024, https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday November 3, 2024 » Earlier function parameters are available to default parameters (#tilPost)., viewed ,<https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Earlier function parameters are available to default parameters (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/
CHICAGO
" » Earlier function parameters are available to default parameters (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/
IEEE
" » Earlier function parameters are available to default parameters (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/. [Accessed: ]
rf:citation
» Earlier function parameters are available to default parameters (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2024/11/03/earlier-function-parameters-are-available-to-default-parameters-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.