0

My javascript has more than 12 functions and all function call the following function.

address_bar(gender, age_min, age_max, religion, country, address, zip-code, telephone);

My question is it is possible to call this function as follows.

var string =  'gender, age_min, age_max, religion, country, address, zip-code, telephone';

Call each function as

address_bar(string); //not working

I get undefined variable errors for parameters.

6
  • 1
    Why don't you create a object and pass that object to function? Commented Apr 14, 2016 at 12:50
  • You could use eval but understand the risks involved before using it... Commented Apr 14, 2016 at 12:50
  • No because you are passing in one parameter that is a string. You can do some magic with split and apply, but if any of the fields have a comma in it, you are in trouble. You would be better off with an array or object. Commented Apr 14, 2016 at 12:50
  • @brso05, eval is evil javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval Commented Apr 14, 2016 at 12:52
  • @Satpal did you read my comment? "but understand the risks involved before using it" there is nothing wrong with using it in certain situations... Commented Apr 14, 2016 at 12:53

4 Answers 4

1

An alternate way could be to put them in an array like

var args =  [gender, age_min, age_max, religion, country, address, zip-code, telephone];
address_bar.apply(null,args)
Sign up to request clarification or add additional context in comments.

7 Comments

why not just call the method directly if you already have everything in variables?
address_bar(gender, age_min, age_max, religion, country, address, zip-code, telephone) is exactly the same, more readable, and less code.
@MarkEvaul But OP is saying that he is calling this method again and again and wants to do some refactoring.
no, he's saying he has 12 points that call this function. He'd have to put this code in 12 places. regardless, doing this, then changing the values of gender, age_min, etc would not change the values in args because javascript doesn't use pointers. You'd have to reset args after changing any of the variables. In that case, calling the method directly would actually be more beneficial because changing the variable then passing the variable to the method would actually work.
Not sure what is the relevance of your fiddle
|
1

You can split the string into an array by the comma, and then pass the resulting parameters into the function via apply, but there's a caveat: All of the parameters would be strings, so if you need to distinguish between numbers and strings, there'd be another step of looping through and parsing the result based on whatever types you're expecting. You'd also have to be careful to ensure that none of the expected values have a comma in them, or you'd be splitting a result prematurely.

Here's an example of the most basic case:

address_bar.apply(null, string.split(','));

1 Comment

assuming you already have the string assembled, this is the correct solution. All the other solutions are no better than just calling the method directly with the variables you already have.
0

You cannot pass them as one string without modifying function, you have atleast 3 options.

1) Change you function to accept one string:

function adress_bar(string){
    //split string and do soemthign with data;
}

adress_bar(string);

2) Change/split data you try to "push" into function like:

var gender = 'gender', age_min = "age_min", ... , telephone = "telephone";

and then run function with each of these variables:

adress_bar(gender, age_min, ... , telephone);

3) Make array or object of your arguments and pass them all as one.

var array = ["gender", "age_min", "age_max", "religion", "country", "address", "zip-code", "telephone"];

function address_bar(some_array){
//do something with some_array
}

address_bar(array);

1 Comment

Don't know why getting downvoted OP tried to pass single string as many arguments so I explained how functions and arguments works.
-1

This does exactly what you want:

var x = [ 'gender', 'age_min', 'age_max', 'religion', 'country', 'address', 'zip-code', 'telephone' ];
address_bar.apply(this, x);

You can read more about apply here :

1 Comment

while technically correct, if you already have all the variables, why not just call the method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.