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
closures - How to define a new global function in javascript - Stack Overflow
html - is it valid to use global function in javascript? - Stack Overflow
Global scope functions in Javascript
Global Scope and Functions
Videos
You could instead call it as window.getNameField:
alert(window.getNameField().value);
Or you could define a variable outside the closure:
var getNameField;
(function(){
getNameField=function(fieldId){
// Code here...
};
}());
alert(getNameField().value);
I would try
window["getNameField"] = function(fieldId) {
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.
I'm assuming you want to filter out native functions. In Firefox, Function.toString() returns the function body, which for native functions, will be in the form:
function addEventListener() {
[native code]
}
You could match the pattern /\[native code\]/ in your loop and omit the functions that match.
As Chetan Sastry suggested in his answer, you can check for the existance of [native code] inside the stringified function:
Object.keys(window).filter(function(x)
{
if (!(window[x] instanceof Function)) return false;
return !/\[native code\]/.test(window[x].toString()) ? true : false;
});
Or simply:
Object.keys(window).filter(function(x)
{
return window[x] instanceof Function && !/\[native code\]/.test(window[x].toString());
});
in chrome you can get all non-native variables and functions by:
Object.keys(window);