问题
In Javascript, is it acceptable to rename built in functions. For example, in my code, I use document.querySelector()
tons of times. This is extremely long and tedious to type every single time (even with autocomplete from the IDE's side). Therefore, I decided to create a new function with a shorter name like the following:
let qs = selector => document.querySelector(selector);
Is this an acceptable practice in the JS community? Is this considered bad code? If so, why? Additionally, if doing this is alright, what are some drawbacks?
Thanks.
回答1:
No. Someone is going to come behind you to edit your code. They will then have to track down your renaming function to actually see what it does. Create an snippet in your IDE if it’s that much of an issue for you.
来源:https://stackoverflow.com/questions/53330996/javascript-renaming-built-in-functions