Home  >  Article  >  Web Front-end  >  Function calls in JavaScript

Function calls in JavaScript

高洛峰
高洛峰Original
2016-11-25 10:57:591320browse

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn