+ 1
Which one to use
I've made a JavaScript file in which I made some kind of library to make some functions be easier. My question is: Is it better to use native JavaScript functions or use constants, so that no variable can have the same name? What are the pros and cons for both native js and constants If you want I can include the code
5 Answers
+ 2
Both can be implemented well. If you use closure, there would not be any name conflict between your library code with other codes. All sealed by closure, you only export some public variables.
+ 1
Run you library codes in closure, to ensure code isolated from the main JavaScript.
+ 1
Thanks for the tip, but should I use functions or constants in that code.
Like this: is it better to do this
function Shuffle(input){
return input.sort(function(){return Math.random()-0.5});
}
To be able to name variables like this function
Or this
const Shuffle = input => {
return input.sort(const = () => {return Math.random()-0.5})
}
To make sure no other variable can be named like this
+ 1
Thanks!
0
By the way, besides the functions I also have some variables that are constant, like PI. Should I let them be var or const? (Pros and cons?)