[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Function passed to function

Posted on 2011-05-10
Medium Priority
Last Modified: 2012-05-11
Hi All,

Can someone explain to me how the attached code works?
I understand the 3 and 4 are passed as the x and y parameters and the function is passed as the z parameter.. But i don understand  what parameters are being passed to: function(x,y) { return x * y}??
Also, what is happening in the following line:


function funcObject(x,y,z) {

function testFunction() {

// third parameter is function
funcObject(3,4,function(x,y) { return x * y})


Open in new window

Question by:oggiemc
  • 3
  • 2

Accepted Solution

Roman Gherman earned 1500 total points
ID: 35731313

Javascript allows you to pass any object as a parameter to a function.

A function is basically an object in javascript, this is why in the example you provided you have a function as the third parameter.

"alert(z(x,y));" - will call the function passed in parameter z with the two parameters: x and y.

this will show an dialog containing the TEXT: 12.  
x*y = 12;
LVL 13

Assisted Solution

dsmile earned 500 total points
ID: 35731332
No params are passed to function(x,y) { return x * y}
It's function definition that is passed to funcObject()

12 will be the result of alert

Author Comment

ID: 35731540
Thanks for reply guys..Unfortunately i still dont see where the actual multiplication of x * y is happening??

\\ "alert(z(x,y));" - will call the function passed in parameter z with the two parameters: x and y.

so what you are saying is that above line is equivalent to:
alert(function(x,y) { return x * y}(x,y));   // i.e z = function(x,y) { return x * y}?

it all looks a bit confusing to me, and im not really sure what is the point of this technique?? Perhaps someone can enlighten me / explain in a bit more detail?
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Assisted Solution

by:Roman Gherman
Roman Gherman earned 1500 total points
ID: 35731629
yes, that is correct.

that syntax is called anonymous function.

Author Comment

ID: 35732781

So all the above line means is:

The z function i.e function(x,y) { return x * y} is called, and the x,y parameters are passed in to it?? I understand it is called an anonymous function, however i do not understand why such functions are used? What is the advantage over declarative functions?


Assisted Solution

by:Roman Gherman
Roman Gherman earned 1500 total points
ID: 35735231
yes the x,y parameters are passed in to it

take a look at this page with explanations: http://helephant.com/2008/08/23/javascript-anonymous-functions/

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question