阅读背景:

如何最好地传递几乎总是相同的可选函数参数

来源:互联网 

What's the best way to pass an optional argument that is nearly always the same? Case in point is Javascript's parseInt function; the second argument for radix is optional, but to avoid a string beginning with a zero being treated as octal, it is generally recognized good practice to always specify 10 as the second argument. Fair enough, so I'm wondering, how to best deal with this situation; specifically, should I create a convenience function, such as parseBaseTenInt(str), that just returns parseInt(str, 10)? I've inherited one file of code with about 10 calls to parseInt without the second argument, and creating the convenience function will subsequently allow me to do a simple replacement of parseInt with parseBaseTenInt. However I can conceive of drawbacks to the convenience function, particularly it not being used by other developers. Because it's just 1 file right now, and I will the primary developer moving forward, it wouldn't be too inconvenient to just add the second argument to all the parseInt function calls.What's the best way to pass an optional argumen




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: