JavaScript Difference between assigning event handler to method with and without parentheses difference between assigning and delegating,difference between assigning work and

Assuming you have the following:

function doStuff() { //code }

What is the the difference between the 2 statements ?

window.onload = doStuff;

window.onload = doStuff();

Both statements immediately called the method, but if I used the first statement, I could treat onload as a function pointer to "doStuff" and just call the method again using:

onload();

Is this the only difference, and in general, is this all guaranteed behavior for event handlers ?

Answer:1

You're actually a bit off in your understanding.

window.onload = doStuff;

means that when the onload event is triggered, the doStuff function will be called.

window.onload = doStuff();

means that when that line of code is reached (before the onload event is triggered), doStuff is executed and it's return result is assigned to the onload handler.

You probably want the first one unless doStuff returns a function to be executed when the onload event is triggered.

Answer:2
window.onload = doStuff();

This will fire immediately as the iterpreter get there.

window.onload = doStuff;

it's a reference to a function which future to be called.

Question :

When should I use window.onload = doStuff();

Answer:

When you need to create closure :

Example :

window.onload = doStuff();

Where

function doStuff()
    {
        var i = 0;
        return function ()
        {
            return i++;
        };
    }
Answer:3

I am trying to get Cheerio to work with Express. I'd like to be able to manipulate the dom from the server, but all I have found is web scraping.. There are some requirements.. At the moment, I am ...

I am trying to get Cheerio to work with Express. I'd like to be able to manipulate the dom from the server, but all I have found is web scraping.. There are some requirements.. At the moment, I am ...

I have one table: <table> <tr id="436"> <td>1</td> <td>2</td> <td>3</td> </tr> </table> Now I need using ...

I have one table: <table> <tr id="436"> <td>1</td> <td>2</td> <td>3</td> </tr> </table> Now I need using ...

  1. javascript change style of element
  2. javascript change style of div
  3. javascript change style of class
  4. javascript change style display
  5. javascript change style sheet
  6. javascript change style color
  7. javascript change style on click
  8. javascript change style of element by id
  9. javascript change style dynamically
  10. javascript change style by class name
  11. javascript change style width
  12. javascript change style background color
  13. javascript change style property of element
  14. javascript change style of all elements of class
  15. javascript change style of pseudo element
  16. javascript change style attribute value
  17. javascript change style tag
  18. javascript change style important
  19. javascript change style of body
  20. javascript change style height

I am trying to implement a course portal where videos will be uploaded locally and will be available to students for studying. I am trying to create a interactive system: which will produce a question ...

I am trying to implement a course portal where videos will be uploaded locally and will be available to students for studying. I am trying to create a interactive system: which will produce a question ...

I'm having trouble with validation of input type="date". It shouldn't accept 18 years old below. Could you please help me? Thank you. I google and search the forum I haven't found the answer. I ...

I'm having trouble with validation of input type="date". It shouldn't accept 18 years old below. Could you please help me? Thank you. I google and search the forum I haven't found the answer. I ...

  1. input type date format
  2. input type date html
  3. input type date value
  4. input type date default value
  5. input type date placeholder
  6. input type date css
  7. input type date safari
  8. input type date set value
  9. input type date time
  10. input type date not working in ie11
  11. input type date not working in ie
  12. input type date min today
  13. input type date polyfill
  14. input type date min max
  15. input type date react
  16. input type date set value jquery
  17. input type date onchange
  18. input type date range
  19. input type date validation
  20. input type date value format