Everything in JS is bound to containing scope. Therefore, if you define a function directly in file, it will be bound to window object, i.e. it will be global.
To make it "private", you have to create an object, which will contain these functions. You are correct that littering global scope is bad, but you have to put something in global scope to be able to access it, JS libraries do the same and there is no other workaround. But think about what you put in global scope, a single object should be more than enough for your "library".
Example:
MyObject = {
abc: function(...) {...},
pqr: function(...) {...}
// other functions...
}
To call abc for somewhere, be it same file or another file:
MyObject.abc(...);
Answer from Marko Gresak on Stack OverflowEverything in JS is bound to containing scope. Therefore, if you define a function directly in file, it will be bound to window object, i.e. it will be global.
To make it "private", you have to create an object, which will contain these functions. You are correct that littering global scope is bad, but you have to put something in global scope to be able to access it, JS libraries do the same and there is no other workaround. But think about what you put in global scope, a single object should be more than enough for your "library".
Example:
MyObject = {
abc: function(...) {...},
pqr: function(...) {...}
// other functions...
}
To call abc for somewhere, be it same file or another file:
MyObject.abc(...);
in test2.js you can write this to make the function global
window.abc = function(){...}
and then in test1.js yo can access it like this
window.parent.abc();
I hope it will help you
I need a simple explanation of global functions within JavaScript, please?
html - is it valid to use global function in javascript? - Stack Overflow
Why does console log print undefined for a global variable in a function?
Adding to the global namespace in Nodejs.
Videos
yes it's valid.
if you don't want to pollute the global scope (and to avoid clash by name) a best practise is to wrap your functions inside an object and expose the object to the global scope
this is an example, but the idea is to bundle together the functions base on their behaviour
<script type="text/javascript">
(function(global){
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
global.bundleObj = {
setCookie: setCookie,
getCookie: getCookie
}
})(window)
</script>
then anywhere in your code
<script>
window.bundleObj.setItem()
</script>
Yeah, that seems fine. If you're using it all over the page, you might as well have it defined in the global scope. The only reason I would recommend otherwise is if you have a lot of functions or you have code intermingling with code other people wrote. If it's pretty short and no one else is contributing, this way is just easier.