Jump to content
Larry Ullman's Book Forums

Ch15 Views.js File Point 12


Recommended Posts

Hello,

I'm going over the "view.js" file on page 585 and in point 12 we have:

"bidAjax.onreadystatechange = handleBidAjaxResponse;"
why "()" is missing in the function call?
Why it is not like this:
"bidAjax.onreadystatechange = handleBidAjaxResponse();"

Thanks

Link to comment
Share on other sites

Because it's not a function call, it's a function reference.

Basically, you assign a function reference to the event handler so that when the event occurs (i.e., not now), the system knows which function to call.

 

If you assigned a function call to the event handler, then the function would be immediately executed, which would likely lead to an unintended result.

 

That make sense?

  • Upvote 1
Link to comment
Share on other sites

Thanks for the answer…
Yes it does, It's described on page 245 of the book in "Functions as Variables Values" paragraph.

Basically in this line "bidAjax.onreadystatechange = handleBidAjaxResponse;" we are assigning value of function "handleBidAjaxResponse" to the property "onreadystatechange" of the object bidAjax.

 

Am I right?

Thanks

Link to comment
Share on other sites

Yes, that's right.

 

Furthermore, properties that have functions assigned to them are often called "methods", but that's purely a semantical differentiation from properties.

As a result though, assigning a function (not a function call) to onreadystatechange essentially makes onreadystatechange a method of the bidAjax object, and that method is called whenever the readystatechange event occurs for the bidAjax object.

Link to comment
Share on other sites

 Share

×
×
  • Create New...