Not quite. JavaScript will use Automatic Semicolon Insertion (ASI) to add a semicolon to the end of the return statement, which is the first closing brace that you have there.
Also to be completely proper, there should be no space between the name of the function and the parenthesis that specify its properties, and since alert is being accessed as a global object, windows.alert is the most proper way of specifying that.
And since we’re attempting to do things the proper way, in light of this it’s time to start using use strict mode article, we can also use “use strict” to tell the interpreter that we want to keep away from using bad parts of the language.
So the most correct way of writing that is:
function foo(message) {
"use strict";
return function () {
window.alert(message);
};
}
var bar = foo('something to say here');
bar();
www.jslint.com can be used to help you find and fix such issues (turn on the “assume a browser” option though), so that you can then become aware of them and apply such techniques to the rest of your code.
With “use strict” you should not apply it outside of a function, for that then forces it to be applied to all of the scripting code of the page, which can also include scripts from external sources such as ads and other parts.
So the best way to use “use strict” is from a top-level function. Your code for example could be contained within an anonymous closure, which helps to sandbox your code and prevent it from affecting the global namespace.
With the above code, the global namespace will have two new variables added to it, foo and bar. This could cause trouble if some other script also defines those variables, and you try to access them later on.
You can protect yourself from such troubles by wrapping your code in a self-executing anonymous closure.
(function () {
"use strict";
function foo(message) {
return function () {
window.alert(message);
};
}
var bar = foo('something to say here');
bar();
}());
Doing things that way now makes the functions local to the anonymous closure, removing them from the global namespace and potential issues thereof.
You may also see code place the function parenthesis outside of the enclosing parenthesis.
(function () {
...
})();
That still works, but the problem that now exists is a matter of semantics. The () are executing the enclosing parenthesis, whereas in all other situations the () are used to invoke the function itself. Placing them inside of the enclosing parenthesis connects them to the function itself, which is more semantically correct than if they are outside and separated from it. Some have come to give a derogatory terms to the “outside of the parenthesis” example as looking like dogs balls, because they are hanging off the very end of things.
If you need to access anything inside of the anonymous function from an external position, you can assign the anonymous function to a single global variable that’s unique to yourself, and return anything that you want to be publically accessible.
window.NICHE = (function () {
"use strict";
function foo(message) {
return function () {
window.alert(message);
};
}
var bar = foo('something to say here');
return {
bar: bar,
otherStuff: 'can be made available too'
};
}());
// later on
window.NICHE.bar();
That’s about the safest way to treat things. The global namespace is similar to public toilet facilities - you should keep your exposure to it to as much of a minimum as is practical.