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 OverflowGlobal Scope and Functions
html - is it valid to use global function in javascript? - Stack Overflow
Global scope functions in Javascript
Defining functions in global javascript tab in geogebra
first of all do developing in 5.x!
This is a standard msg if there is some error in code or data...
start with a simple alert-msg in global code
do developing in a click script - if running copy to global tab
More on reddit.comVideos
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(...);
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
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.
Hi, I just startet scripting with javascript in geogebra and ran into different problems.
Is it possible to define global functions (inside the Global Javascript tab) that can be called from different objects (for example multiple buttons)? I tried it but didnยดt get my solution to work.
I have a text object ("text1") and a button object ("button1"). I defined a function ("doSomething") inside the Global Javascript tab. The function "doSomething" is called inside the On Click tab of button1. But when button1 is clicked, I get the error message "doSomething is not defined".
first of all do developing in 5.x!
This is a standard msg if there is some error in code or data...
start with a simple alert-msg in global code
do developing in a click script - if running copy to global tab
You need to save and reload the file for the Global JavaScript to run
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) {
In Chrome, go to Dev tools and open the console. Then type in the following:
Object.keys( window );
This will give you an Array of all the global variables.
After searching on Google a bit, I found a way. You will need Firefox and the jslinter addon.
Once setup, open jslinter and go to Options->check everything on the left column except "tolerate unused parameters".
Then run jslinter on the webpage and scroll down in the results. You will have a list of unused variables (global and then local to each function).
Now run Object.keys(window); in the console and compare the results from both to figure out which ones are used.
This one-liner will get you pretty close, and does not require installing anything additional, or running code before the page loads:
Object.keys(window).filter(x => typeof(window[x]) !== 'function' &&
Object.entries(
Object.getOwnPropertyDescriptor(window, x)).filter(e =>
['value', 'writable', 'enumerable', 'configurable'].includes(e[0]) && e[1]
).length === 4)
It filters Object.keys(window) based on three principles:
- Things that are null or undefined are usually not interesting to look at.
- Most scripts will define a bunch of event handlers (i.e. functions) but they are also usually not interesting to dump out.
- Properties on window that are set by the browser itself, are usually defined in a special way, and their property descriptors reflect that. Globals defined with the assignment operator (i.e.
window.foo = 'bar') have a specific-looking property descriptor, and we can leverage that. Note, if the script defines properties using Object.defineProperty with a different descriptor, we'll miss them, but this is very rare in practice.