Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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

This comment has been minimized.

Copy link

Kremnari commented Aug 8, 2020

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

@itaditya

This comment has been minimized.

Copy link
Owner Author

itaditya commented Aug 8, 2020

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

@Kremnari

This comment has been minimized.

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

This comment has been minimized.

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
You can’t perform that action at this time.