Function calls in JavaScript

Source: Internet
Author: User

First, define the function:
View sourceprint? Function Hanshu ()

{

// Function body...

}

In this way, we define a function named Hanshu. Now we try to call this function. In fact, it is very simple. To call a function, use the name of the function to add brackets, that is:

Hanshu ();

In this way, we call this function.

Call this function with a button: point the button clicking event to this method.

<Input type = "button" value = "click me" onclick = "Hanshu ()">

Now I will write another method in this method.

In this method, we direct the Click Event of the body to a new method named anonymous. Click body
View sourceprint? Function Hanshu ()

{

Document. body. onclick = function ()

{

Alert (click body );

};

}

Now let's try to call

<Input type = "button" value = "click me" onclick = "Hanshu ()">

However, the click body dialog box appears when you click the body.

Because this method cannot be found when the body accesses this anonymous method

We can test it like this.

View sourceprint? Function AddClick ()

{

// In this way, the Click Event of the body cannot be assigned to a new method, because the body cannot access the function in the function body.

/*

Document. body. onclick = new function ()

{

Alert (click body );

};

*/

// This can be achieved

Document. body. onclick = BtnAn;

// If you direct the method to the method in the function, the following error occurs: NeiHanshu is not defined.

// Document. body. onclick = NeiHanshu;

}

Function BtnAn ()

{

Alert (click body );

// Function NeiHanshu ()

//{

// Alert (the function in the function is called );

//}

// NeiHanshu ();

}

So the result is: we can only call external functions, but cannot call functions in the functions. This is an access level problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.