JS Tutorials
JS Objects
JS Functions
JS Classes
JS Async
JavaScript functions are executed in the sequence they are called. Not in the sequence they are defined.
The apply()
method is similar to the call()
method (previous chapter).
In this example the fullName method of person is applied on person1:
function myFirst() {
myDisplayer("Hello");
}
function
mySecond() {
myDisplayer("Goodbye");
}
myFirst();
mySecond();
The difference is:
Sometimes you would like to have better control over when to execute a function.
Suppose you want to do a calculation, and then display the result.
call()
method takes arguments
separately.
You could call a calculator function ( myCalculator
) , save the result, and then call another function
(myDisplayer
) to display the result:
A callback
is a function passed as an argument to another function.
Using a callback, you could call the calculator function (myCalculator) with a callback, and let the calculator function run the callback after the calculation is finished:
he examples above are not very exciting.
They are simplified to teach you the callback syntax.
Where callbacks really shine are in asynchronous functions, where one function has to wait for another function (like waiting for a file to load).
Asynchronous functions are covered in the next chapter.