How to leave this function javascript

Asked

Viewed 154 times

3

I have this function below and I would like to leave it generic, because the values of innermap, -125,127 and 3 will vary.

Instead of creating multiple functions: centralizar(); centralizar1(); etc., I would like to leave only one function and go passing only the parameters to the next functions where the values will be different.

How to do this?

function centralizar() {
    innermap.flyTo([-125,127], 3);
}
  • Hello Isa! The answer below solved your problem? Give feedback to us. Thank you!

  • @Is the answer solved your problem? You need something improved?

1 answer

9

Using parameters

function centralizar(innermap, a, b, c) {
  innermap.flyTo([a, b], c);
}

Or maybe use a array instead of b, c. Kicking the parameters are coordinates and zoom, would be

function centralizar(map, coords, zoom) {
  innerMap.flyTo(map, coords, zoom);
}

Tip from bfavaretto in the comments.

Job documentation

  • I do not miss the 4 parameter q she asked "innermap" that would be one (a,b,c,d) ?

  • if changing function name can do var nomedafuncao ='innermap'; window[nomedafuncao](a,b,c)

  • 1

    I would do centralizar(map, coords, zoom) - kicking that the third is zoom.

  • Hello! First, don’t stress yourself, it’s a normal debate. If I send centralizar('qualquercoisa', '-125,127', '3') your function will work?

  • @But not because the two parameters (a, b) are numbers to form the array and not a string. Should be centralizar('qualquercoisa', -125, 127, '3'). And I don’t think I understood the first sentence.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.