Jump to content

Javascript Function Definition


toreachdeepak

Recommended Posts

Hi,I am generally using the function definition of the Javascript function as given belowfunction abc(){..............................}But in the Yahoo user interface it is given as YAHOO.example.BasicRemote = function() {}();Is it correct as per Javascript standards ? ThanksDeepak

Link to comment
Share on other sites

What they're doing there is assigning the return value of an anonymous function to the variable. Here's an example of a use for it:

var Population = function() {  return xmlDoc.getElementsByTagName("person").length}();alert(Population); // Returns the amount of <person> nodes in the specified XML document

Notice the (); at the end. That's what's making the preceding function run. If they were absent, you'd have to apply them to the variable when using it:

var Population = function() {  return xmlDoc.getElementsByTagName("person").length};alert(Population());

Link to comment
Share on other sites

That technique is used to keep things in their own scope. You can make global variables in that function that are only global inside the function. That helps if you're defining other functions inside the function, like a class.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...