How to call this default args function more than once?

Clash Royale CLAN TAG#URR8PPP
How to call this default args function more than once?
I have encountered a question where I need to allow default args to be set on a function in JavaScript:
function dfltArgs(func, params)
const strFn = func.toString()
console.log(strFn)
const args = /(([^)]+))/.exec(strFn)[1].split(',')
const defaultVal = (arg, val) => typeof arg !== 'undefined' ? arg : val
return (...dynamicArgs) =>
const withDefaults = args.map((arg, i) =>
defaultVal(dynamicArgs[i], params[args[i]]))
return func(...withDefaults)
function add (a, b) return a + b
var add_ = dfltArgs(add,b:9)
console.log(add_(10)) // Should be 19
var add_ = dfltArgs(add_,b:3)
console.log(add_(10)) // Should now be 13
However, I need to be able to call this function more than once and overwrite previously set defaults:
var add_ = defaults(add,b:9)
add_(10) // Should be 19
var add_ = defaultArguments(add_,b:3)
add_(10) // Should now be 13
This does not work in my implementation, because the stringified function on the second call is: (...dynamicArgs) => {, etc.
(...dynamicArgs) => {
How can I refactor this? Probably need to use bind somehow?
bind
Function.bind()
var add = function( x, y ) return x + y; ; var add5 = add.bind( null, 5 );
Please do not vandalize your posts. If you believe your question is not useful or is no longer useful, it should be deleted instead of editing out all of the data that actually makes it a question. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 3.0 license). By SE policy, any vandalism will be reverted.
– André Kool
Aug 8 at 12:21
@AndréKool Delete it then.
– Jon Doe
Aug 8 at 12:39
Locking. Andre is right. Could you let us know why you're trying to deface your own post?
– George Stocker♦
Aug 8 at 13:37
Editing Questions to improve them (e.g. clarification, adding additional information, etc.) is encouraged. However, editing a Question to change it into a different question which results in invalidating an Answer, is against policy on Stack Overflow. Your edit here did so. The policy is that other users should proactively revert such changes. I have done/will do so here. You are encouraged to ask a new Question, perhaps with a link to this one for additional context. We want to help, but your new/additional issue needs to be a new Question.
– Makyen
Aug 9 at 22:42
1 Answer
1
Instead of your complicated default args thing, why not just use some arrow functions with real default arguments:
var _add = (a, b = 8) => add(a, b);
That way you can easily change the bound things:
var add_ = (a = 2, b) => _add(a, b);
add_() // 10
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Function.bind()does exactly this, create a new function with some parameters and/or the 'this' context bound to fixed values:var add = function( x, y ) return x + y; ; var add5 = add.bind( null, 5 );– Shilly
Aug 8 at 11:44