3 ms·
Indeed. What that pattern also solves, and AFAIK most static languages lack, is argument forwarding (though Python solves this even more elegantly than JS):
by tomp 7y ago
Indeed. 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.