In this section, we will introduce important features of functions in JavaScript, as well as three ways to declare a function.
Function is value
In JavaScript, a function is a value, the same as a string or a number.
A declaration creates a function and writes a reference to it in a variable .
Like any value, the function can be output like this:
Here it is not the result of the sayHi()
function that is sayHi()
way, what is it equal to? .. correct, undefined
, because there is no return
), but the function itself, i.e. its code.
A function is not just a value, it is an object.
You can even write a property to it:
This feature is rarely used.
Copying functions
The function can be copied to another variable.
At the same time, since a function is an object, it is copied “by reference”.
That is, the function itself lies somewhere in memory, and the variable contains the “address” where it is located. When assigning func = sayHi
this address is copied, both variables begin to point to the same “place in memory” where the function is located.
After copying it can be called:
Note again that the call of the form alert(func)
is different from alert( func() )
. In the first case, the function (its code) is output, in the second, this function is called, and its result will be output.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone