referencing functions, calling functions, revealing

Source: Internet
Author: User

    • Problem Introduction : When you look at the fourth edition of the basic jquery tutorial, P34 page has such a passage

referencing functions and calling functions

Here, when the function is specified as a handler, the following parentheses are omitted and only the function name is used. With parentheses, the function is called immediately; without parentheses, the function name is simply a function identifier or a reference to a function that can be used to invoke the function in the future.

This raises my mind about the way the binding event works. We all know that when binding an event handler for an element, the inline way is to write <body onload= "dosomething ();" >...</ Body > , or JavaScript code to write like this

1 window.onload=dosomething;//or the next line way 2 window.addeventlistener (' Load ', dosomething, false ); 3 function dosomething () {4    ... 5  }

We have always used this, but have not thought about why the onload= "dosomething ( )" To add "()"? Can I give it no? , and the JS code of onload and AddEventListener Why not Add "()", I can add it to it?

    • question thinking : As the above-mentioned "()" is called function, JS code execution to that line when the function is immediately called and executed. Without "()" is a reference function, the window.onload property simply points to the dosomething function Body (why it is pointing instead of assigning a simple function body to the OnLoad property to be verified later), When the onload event is triggered in the future,dosomething is added to the task queue waiting for the main function to execute it.
    • Problem Verification :

1. In the standard inline mode, the console will output "test" as promised.

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4   <title></title>5 </Head>6 <Bodyonload= "dosomething ()">7 <Scripttype= "Text/javascript">8   functiondosomething () {9 Console.log ('Test');Ten   } One </Script> A </Body> - </HTML>

2. Inline mode does not add "()"

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4   <title></title>5 </Head>6 <Bodyonload= "DoSomething">7 <Scripttype= "Text/javascript">8   functiondosomething () {9 Console.log ('Test');Ten   } One </Script> A </Body> - </HTML>

The result console does not output and does not prompt the error, to break the point to see what happened to the program, to the main code each line to break the breakpoint

Then rerun the program, the program to line 6th, pause.

Click Continue execution, the program does not execute the 9th line of code but run directly finished. And then look at the document.body.onload property Output is

So this means that when the onload event is triggered, the function onload is executed, the contents of which are dosomething , not dosomething. () , so the onload event does not execute after the something function is triggered just after the trigger enters function onload and ends.

3. Standard JS binding event mode, Window.onload and AddEventListener, the former example

referencing functions, calling functions, revealing

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.