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
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) {
You can make your function globally accessible and still keep reference to variables in the scope in which it was created. Simply create and assign it in window scope - e.g. instead of defining it as:
function autoSave() {
// ... code ...
}
declare it as:
window.autoSave = function() {
// .... code ....
}
you will now be able to call it anywhere (provided the initStage method has been called to declare it first, of course).
You can assign the autoSave function to the this object, i.e.
function initStage() {
... Some Code ...
this.autoSave = function(){
... Some Code ...
}
return this;
}
Now you can call
initStage().autoSave();
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.
You have two options, add it to the window object to make it global:
window.getList = function(url, gkey){
// etc...
}
or move it from inside the document ready event handler into the global scope:
$(document).ready(function() {
alert(getList('http://www.youtube.com/watch?v=dm4J5dAUnR4', "v"));
});
var getList = function(url, gkey){
var returned = null;
if (url.indexOf("?") != -1){
var list = url.split("?")[1].split("&"),
gets = [];
for (var ind in list){
var kv = list[ind].split("=");
if (kv.length>0)
gets[kv[0]] = kv[1];
}
returned = gets;
if (typeof gkey != "undefined")
if (typeof gets[gkey] != "undefined")
returned = gets[gkey];
}
return returned;
};
You might also want to read this question about using var functionName = function () {} vs function functionName() {}, and this article about variable scope.
Yet another option is to hang the function off the jQuery object itself. That way you avoid polluting the global name space any further:
jQuery.getlist = function getlist(url, gkey) {
// ...
}
Then you can get at it with "$.getlist(url, key)"