Variable function name Javascript.
Posted
by Vladimir
on Stack Overflow
See other posts from Stack Overflow
or by Vladimir
Published on 2010-06-08T10:57:26Z
Indexed on
2010/06/08
11:02 UTC
Read the original article
Hit count: 208
I'm sorting array:
myArray.sort(comparators.some_comparator);
and I have several comparator to choose from:
comparators = {
asc_firstname_comparator : function(o1, o2){
...
}
desc_firstname_comparator : function(o1, o2){
...
}
etc...
}
I want to write function which returns certain comparator depending on input data. It should figure out comparator from string inputs, something like this:
function chooseComparator(field, order){
return "comparators."+order+"_"+field+"_comparator";
}
So is it possible to pass only function name string to sort()
method or I'll need to pass reference to correct comparator somehow?
© Stack Overflow or respective owner