Skip to content

Instantly share code, notes, and snippets.

@itaditya
Last active August 8, 2020 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itaditya/de2f01d2a3830b235b3f16d713b724d4 to your computer and use it in GitHub Desktop.
Save itaditya/de2f01d2a3830b235b3f16d713b724d4 to your computer and use it in GitHub Desktop.
(Blog) Why named arguments are better than positional arguments
// Implementation
function myCustomParseInt(objArgs) {
return parseInt(objArgs.item, objArgs.radix);
}
// Usage
const num = myCustomParseInt({ item: '100', radix: 10 });
@Kremnari
Copy link

Kremnari commented Aug 8, 2020

You might want to choose ".value" or ".item" for the passed object, not both :D

@itaditya
Copy link
Author

itaditya commented Aug 8, 2020

Good Catch @Kremnari, fixed it. Thanks a lot!

@Kremnari
Copy link

Kremnari commented Aug 8, 2020

Named arguments are an interesting pattern. I'm considering using it as part of a "dependency injection" for things like MobX actions.
I have wondered what the performance comparison is vs positional arguments though

@Kremnari
Copy link

Kremnari commented Aug 8, 2020

Sorry, should've looked first...
https://jsperf.com/object-vs-positional-args
Surprisingly, nearly no difference on chrome mobile

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