1

Is there a way nest functions and pass them as a parameter? I have this 2 functions:

function mayus(string) {
    return string.toUpperCase();
}

function removeA(string) {
    return string.replace(/A/g, "");
}

and I want to apply them in 2 different ways

function stringHandler1(stringID) {
    var newString = mayus(removeA(getString(stringID))); 
    return newString;
}

function stringHandler2(stringID) {
    var newString = removeA(mayus(getString(stringID)));
    return newString;
}

The 2 stringHandlers return slightly different strings, but they are fairly similar. Is there a way to use one string handler that takes 2 parameters (the stringID and the operation)

function stringHandlerINeed(string, operation) {
    var newString = operation(string);
    alert(newString);
}

I want to do something like

stringHandlerINeed("516af2", mayus(removeA))

Update: I marked jJ' as the answer because it doesn't require me to change any of the functions I already have, and it's the concept I was looking for, but this answer by juvian is so simple makes me wish I had come up with it.

0

3 Answers 3

4

Best way I can think of is sending the functions in an array according to the order of the operations, and then evaluate each one at a time:

function stringHandlerINeed(string, operation) {
    var newString=string;
    for(var i =0; i< operation.length;i++){
        newString=operation[i](newString);
    }
    alert(newString);
}


stringHandlerINeed("516af2", [mayus,removeA]) // alerts 516F2
Sign up to request clarification or add additional context in comments.

Comments

2

What you really want is probably function composition... one way to implement it is like this:

function compose() {
    var fns = Array.prototype.slice.call(arguments);
    return function (arg) {
        fns.forEach(function (fn) {
            arg = fn(arg);
        });
        return arg;
    };
}

compose(mayus, removeA)('axA'); // -> "X"
compose(removeA, mayus)('axA'); // -> "AX"

There are endless applications of this concept. You can use any number of functions chained in compose. You can name the composed function:

var mayusRemoveA = compose(mayus, removeA);

You can also create a constant function (something like your handler above) instead of values that are evaluated by calling composed function directly...

var theHandlerINeed = compose(function () { return 'axA'; }, mayus, removeA);

theHandlerINeed(); // -> "X"

Comments

1

I was going the same direction of juvian, but simpler.

In this jsfiddle (http://jsfiddle.net/4J2AH/), I pass in the string and functions I want to operate on, then do those operations and return the new string.

The problem with stringHandlerINeed("516af2", mayus(removeA)) is that when you call mayus(removeA), this means you are actually invoking the mayus function which is passing in another function. When it goes to upper case a function, javascript will throw an error.

Comments

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.