Home >Web Front-end >JS Tutorial >Function calls in JavaScript
First let’s talk about the definition of the function:
view sourceprint?function Hanshu()
{
// Function body...
}
In this way we have defined a function named Hanshu, now we try to call this function. In fact, it is very simple. To call a function, just use the function name plus parentheses, that is:
Hanshu();
In this way, we call this function.
Let’s call this function with a button: point the click event of the button to this method
Now I am here Write another method in the method
In this method, we point the body's click event to a new method, the anonymous method. Then click body
view sourceprint?function Hanshu()
{
document.body.onclick = function()
{
alert(click body);
};
}
Now we try to call Click
But this cannot be achieved every time the body is clicked, the click body will pop up
because when the body goes to access this When using an anonymous method, this method cannot be found
We can test it like this
view sourceprint?function AddClick()
{
// In this way, the click event of the body cannot be pointed to the new method, because the body cannot Access the function in the function body
/*
document.body.onclick = new function()
{
alert(click body);
};
*/
// This works Implementation
Document.body.onclick = BtnAn;
// If you point the method to a method within the function, an error will occur: NeiHanshu is undefined
// document.body.onclick = NeiHanshu;
}
function BtnAn()
{
alert(click body);
// function NeiHanshu()
// {
// alert(in function The function is called);
// }
// NeiHanshu();
}
So the result is: we can only call functions outside, but not functions inside functions, that is An access level question