Please pardon my memory if I have this incorrect... Javascript isn't my preferred implementation language.
There are several reasons why one would want to have a no arg function wrap another function call. While the simple call to window.alert("Hello");
is something that you could imagine just instead calling directly instead of sayHello()
.
But what if there is more to it? You've got a dozen places where you want to call sayHello()
and have written window.alert("Hello");
instead. Now you want it to do a window.alert("Hello, it is now " + new Date())
. If you wrapped all those calls as sayHello()
you change it one place. If you didn't, you change it in a dozen places. This touches on Don't Repeat Yourself. You do it because you don't want to have to do it a dozen times in the future.
I worked with an i18n / l10n library in the past that used functions to do client side localization of the text. Consider the sayHello()
function. You could have it print out hola
when the user is localized to a Spanish language. This could look something like:
function sayHello() {
var language = window.navigator.userLanguage || window.navigator.language;
if(language === 'es') { window.alert('Hola'); }
else { window.alert("Hello"); }
}
Though, this isn't how the library worked. Instead, it had a set of files that looked like:
# English file
greeting = hello
# Spanish file
greeting = hola
And then the library would detect the browser language setting and then create dynamic functions with the appropriate localization as the return value for a no-arguemnt function call based on the appropriate localization file.
I'm not enough of a Javascript coder to say if that is good or bad... just that it was and can be seen as a possible approach.
The point being, wrapping the call to another function in a function of its own is often quite useful and helps with the modularization of the application and may also result in easier to read code.
All that bit aside, you are working from a tutorial. It is necessary to introduce things as simply as possible at the start. Introducing varargs style function calls from the start can result in some very confusing code for a person who is unfamiliar with coding in general. It is much easier to go from no argument, to arguments, to varargs style - with each building on the previous examples and understanding.