Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a parameter to a function without it running right away?

I'm having somewhat of an odd issue with trying to piece together a somewhat dynamic Google Maps display. I have overlays on a map that I would like to call a function when clicked. Initially I had everything hard coded, so I had a function for each overlay like this:

google.maps.event.addListener(southEast, 'click', showSouth); function showSouth() {    // do stuff } 

This worked without a problem, but then I made the whole page more dynamic so I decided to make one function that would pass an ID and then display based on that, which is how I feel it should have been set up originally anyway. I altered the code to look more like this:

google.maps.event.addListener(southEast, 'click', showArea(1)); function showArea(id) {    // do stuff based on that id } 

The function worked, but the problem is it gets called immediately on page load. After researching I've learned that when you call a function with the parentheses, that function gets called immediately and then it's return value is referenced. source

So now I'm a little stuck as to how exactly to go about passing that ID to the function without having it call the function immediately. I've found some hacky ways of doing it that might work, but I feel like this shouldn't be something I have to hack together...

like image 909
Matt McClure Avatar asked Mar 09 '12 17:03

Matt McClure


People also ask

How can parameters be passed to a function?

Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.

What are the ways to pass parameters to and from procedure?

To pass one or more arguments to a procedure In the calling statement, follow the procedure name with parentheses. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

When a parameter is passed to a function it is called?

The most common evaluation strategy when passing arguments to a function has been call by value and call by reference: call by value. The most common strategy is the call-by-value evaluation, sometimes also called pass-by-value.

How do you pass a parameter to a variable?

If you want the called method to change the value of the argument, you must pass it by reference, using the ref or out keyword. You may also use the in keyword to pass a value parameter by reference to avoid the copy while guaranteeing that the value will not be changed. For simplicity, the following examples use ref .


Video Answer


2 Answers

Have showArea return a function that works with the id.

function showArea(id) {    return function() {        // do stuff with id    }; } 

The returned function closes over id so it continues to reference it, and is passed to addListener to be used as the handler.


Alternately, you could just inline the function that calls showArea(1)...

google.maps.event.addListener(southEast, 'click', function() { showArea(1); }); function showArea(id) {    // do stuff based on that id } 

This will work because you're hardcoding the 1. If it was a variable that could change, like in a loop, you'd use the first example.

like image 68
user1106925 Avatar answered Oct 30 '22 10:10

user1106925


Try using bind().

You could also use bind() that binds the function and allows you to pass parameters to that method, without running the method on initialization.

google.maps.event.addListener( southEast, 'click', showArea.bind( this, 1 ) ); 

With bind(), the first parameter is always the context (e.g. this) and any other parameters will be passed to the method itself. So,

  • your first method parameter is passed as the second parameter in bind,
  • your second method parameter is passed as the third parameter in bind,
  • etc.

Note, I'm not a Javascript expert so not sure if there are any implications with this strategy that I'm overlooking.

like image 38
Joshua Pinter Avatar answered Oct 30 '22 09:10

Joshua Pinter