Differences between window. onscroll = move and window. onscroll = move ()

Source: Internet
Author: User

Event = a function with parentheses gets the return value. event = a function without parentheses assigns the function itself to the event. event = "function with parentheses" indicates the execution function.
<HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <br/> <title> untitled document </title> <br/> <MCE: script language =" JavaScript "> <! -- </P> <p> function move () <br/> {<br/> var Top = 100 + document. body. scrolltop; <br/> document. getelementbyid ("adlayer "). style. pixeltop = top; <br/> return top; <br/>}< br/> window. onscroll = move; <br/> function showmove () <br/>{< br/> var movevalue = move (); <br/> alert (movevalue ); <br/>}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <Div style = "height: 1900px; Background-color: black "> </div> <br/> <Div id =" adlayer "style =" position: absolute; top: 100px; left: 50px; Z-index: 2 "> <br/> <br/> <input type = "button" value = "button" onclick = "showmove () "> <br/> </div> <br/> </body> <br/> </ptml> <br/>

 

After reading this example, let's look at the following description:
First
VaR movevalue = move (); here we define the variable and assign the move () value to the variable. Then, the value obtained by movevalue is the retrun value returned after the move () function is executed.
Next
Window. onscroll = move; I want to clarify first that VaR move = function () {function content} is equivalent to function move () {function content}, that is, the variable move is the function itself. then we will try again window. onscroll = move to understand, we are assigning the "function itself" to the window event onscroll; that is
Onscroll indicates funtion move () {function content} instead of the value returned by the function;
Last
Onclick = "showmove ()" here is the event execution function. The event is not equal to or equal to the retrun value of the function. It only executes a function when the event is triggered.

Through comparison, we can easily understand the differences between the three. The function with event = parentheses gets the return value, and the function with event = Not parentheses assigns the function itself to the event, event = "function with parentheses" is the execution function
Another important point is that onclick = "showmove" is the HTML event mechanism, while var movevalue = mone () and window. onscroll = move are javscript.

 

Reference: http://www.zhaowenlong.com/post/2010/09/2.html

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.