4 ms·
Just use an object with keys as the parameter names, this pattern is actually very common so I don't see the issue. doStuff({ foo: 3, bar: 7 })
by enlyth 7y ago
Just use an object with keys as the parameter names, this pattern is actually very common so I don't see the issue.
doStuff({ foo: 3, bar: 7 })
- tomp 7y agoIndeed. What that pattern also solves, and AFAIK most static languages lack, is argument forwarding (though Python solves this even more elegantly than JS): def f(x, **kwargs): return g(x, foo=1, **kwargs)
- zeroxfe 7y agoIsn't the JS version just: function f(x, ...args) { return g(x, ...args); } Or did you mean forwarding object fields? function f(x, params) { return g(x, {foo: 1, ...params}); }
- tomp 7y agoAFAIK the first only works for positional (non-keyword) arguments. The second works, I guess? Not an expert in JS, especially not the "new parts", splats are definitely an improvement!
- garyclarke27 7y agoJS parameters are named to some extent, as in you can refer to them by name within the function body - BUT (a critical shortcoming in my view) you can’t assign values by name when calling functions, you can only assign values by position.
- jakear 7y agoSyntax special-cased to function parameters/arguments is more elegant than reusing the same object&spread syntax everywhere to achieve the same effect? I don't think so.
- saagarjha 7y agoC++ has argument forwarding among its many metaprogramming tricks.
- garyclarke27 7y agoYes an object is better than nothing, but is awkward syntax compared to proper named parameters and I don’t see it being used very often. In Postgres I find named paremeters far more convenient than positional paremters for calling functions, also conveys intention much better, also great for defaults (you can use default=null to get around ordering retstriction Postgres has).
- valuearb 7y agoYep, typeless Javascript can do anything except catch errors.