The collection of variables and their values that exist at a given time is called the environment. When a program starts up, this environment is not empty. It always contains variables that are part of the language standard, and most of the time, it has variables that provide ways to interact with the surrounding system. For example, in a browser, there are variables and functions to inspect and influence the currently loaded website and to read mouse and keyboard input.
Qui Marijn la fa lunga, più del necessario per un corso introduttivo. Inoltre fa spesso riferimento al browser che io non uso (qui). Ma le cose fondamentali saltano comunque fuori.
A lot of the values provided in the default environment have the type function. A function is a piece of program wrapped in a value. Such values can be applied in order to run the wrapped program. For example, in a browser environment, the variable
alert holds a function that shows a little dialog box with a message. It is used like this:
Executing a function is called invoking, calling, or applying it. You can call a function by putting parentheses after an expression that produces a function value. Usually you’ll directly use the name of the variable that holds the function. The values between the parentheses are given to the program inside the function. In the example, the
alert function uses the string that we give it as the text to show in the dialog box. Values given to functions are called arguments. The
alert function needs only one of them, but other functions might need a different number or different types of arguments.
alert function can be useful as an output device when experimenting, but clicking away all those little windows will get on your nerves. In past examples, we’ve used
When running the examples, or your own code, on the pages of this book,
Though variable names cannot contain period characters,
console.log clearly has one. This is because
console.log isn’t a simple variable. It is actually an expression that retrieves the
log property from the value held by the
console variable. We will find out exactly what this means [prossimamente].
Showing a dialog box or writing text to the screen is a side effect. A lot of functions are useful because of the side effects they produce. Functions may also produce values, and in that case, they don’t need to have a side effect to be useful. For example, the function
Math.max takes any number of number values and gives back the greatest.
Math.min, which is the opposite of
Math.max, is used as an input to the plus operator:
Prossimamente vedremo come scrivere le nostre funzioni.
Browser environments contain other functions besides alert for popping up windows. You can ask the user an OK/Cancel question using
confirm. This returns a Boolean:
true if the user clicks OK and
false if the user clicks Cancel.
confirm("Shall we, then?");
prompt function can be used to ask an “open” question. The first argument is the question, the second one is the text that the user starts with. A line of text can be typed into the dialog window, and the function will return this text as a string.
prompt("Tell me everything you know.", "...");
These two functions aren’t used much in modern web programming, mostly because you have no control over the way the resulting windows look, but they are useful for toy programs and experiments.