Skip to content Skip to sidebar Skip to footer

Why Can't I Invoke A Function Directly?

I am working through a small exercise from code school and cant figure out why I have to pass the function buildTicket(parkRides, fastPassQueue, wantsRide); to a variable and then

Solution 1:

Your buildTicket function returns a function that does not use a parameter.

For instance, in your code, it returns with a function here:

 return function(){alert("Quick you have a fast pass to "+pass+"!");
        };

You could invoke the returned function immediately with this syntax:

buildTicket(parkRides, fastPassQueue, wantsRide)();

You could also change the return lines in function buildTicket to immediately alert instead of returning a function, but that could break other uses of buildTicket that expect it to return a function thereby delaying that alert to possibly display at a particular point in the code later when it is needed.


Solution 2:

This should work fine!

buildTicket(parkRides, fastPassQueue, wantsRide)();

Solution 3:

When you use the variable ticket(); you are doing one more function call than you would do otherwise.

Basically the first buildTicket call by itself... buildTicket(parkRides, fastPassQueue, wantsRide);

looks like...

(function buildTicket(allRides, passRides, pick) {
    //... your code here that is returning a function
}(parkRides, fastPassQueue, wantsRide);

under the hood, and will return an anonymous function containing an alert that has not been invoked yet. Looking like...

(function () {
    //... your code containing an alert
});

and since it has not been bound to a variable it is just sitting there unable to be invoked because you have no references to it now. There are 2 ways around this problem: 1) you have already figured out which is assigning the anonymous function to a variable and invoking the function using the variable. 2) Is to immediately invoke the returned function by adding another set of parens.

In the end they both do the same thing and will end up giving you the alert via

(function () {
    //... your code containing an alert
})();

Also, if all you want is to invoke the alert, you can forget about the extra function statement and the return call and just have

function buildTicket(allRides, passRides, pick){
    if(passRides[0]==pick){
        var pass = passRides.shift();
        alert();
    }else {
        for(var i = 0; i<allRides.length; i++){
            if(allRides[i][0] == pick){
                alert();                                    
            }
        }
    }
}

Post a Comment for "Why Can't I Invoke A Function Directly?"