3 ms·
Other languages have solved it using better methods. 1. Declare another function with same name and make the older function a wrapper with a default for new ar
by harikb 6y ago
Other languages have solved it using better methods.
1. Declare another function with same name and make the older function a wrapper with a default for new argument
2. Support for an explicit default argument
Either of these would have prevented GP from having their phone bug/problem
- jakelazaroff 6y agoPresumably OP didn’t want to create a new function, or they would have. JavaScript supports default parameters, but again, presumably OP wanted the caller to supply all the arguments. If you’re trying to change the signature of an existing function, I don’t think we’ve yet figured out a better safeguard than static typing.
- colejohnson66 6y agoIt’s possible to use "arguments.length" to “change” the function signature and execute different code for different parameter counts. It’s a hack, but it works.
- jchw 6y agoThe first solution can be done in JavaScript and it is done sometimes, but other times it may be undesirable, especially for refactoring. The second solution was done; ECMAScript (since the 2015 edition) supports explicit default arguments. When you add TypeScript into the mix, it can help a lot as it is often able to detect under- and over-application as type errors (though not always, due to intentional looseness in the language; TypeScript is not fully sound.)