0

There are two functions one uses first class and assigning a function to a variable then returns the variable and the other one is just a regular simple function. In there, I don't get why is one better than the other one because they both looks and does the exact same thing to me. Can someone please give me a hand giving me an easier understanding the difference?

first class

function createDrinkOrder1(passenger) {
    var orderFunction;
    if (passenger.ticket === "firstclass") {
        orderFunction = function() {
            console.log("Would you like a cocktail or wine?");
        };
    } else {
        orderFunction = function() {
            console.log("Your choice is cola or water.");
        };
    }
    return orderFunction;
}

simple function

function createDrinkOrder2(passenger){
    if(passenger.ticket === "firstclass"){
        console.log("Would you like a cocktail or wine?");
    }else{
        console.log("Your choice is cola or water.");
    }   
}
Gavriel
  • 18,880
  • 12
  • 68
  • 105
Tsuna
  • 2,098
  • 6
  • 24
  • 46
  • The first actually returns something that can be used. The second just prints to console. – zero298 Jan 28 '16 at 23:32
  • @zero298 but what it's returned, it's still console prints why not just use `return console.log..........` instead of using a function for it? – Tsuna Jan 28 '16 at 23:35
  • 1
    You can use the returned function multiple times without having to call `createDrinkOrder1` functions again. – zero298 Jan 28 '16 at 23:41
  • @KaiQing: What do you mean? – Bergi Jan 28 '16 at 23:45
  • @Bergi - I mean console.log and IE not getting along together. Even if Edge no longer explodes if you leave a console.log in your js without the console open, only like 80% of corporate idiocy and MS contracts bind massive networks to ancient IE browsers because it takes 4 years to get a technician approved to update hardware or software. Therefore, when IE sees console.log, it cries and gives up unless Uncle console window happens to be open to talk it out of suicide, but yet fail to warn poor IE it will die if Uncle console window isn't there to save it. – Kai Qing Jan 29 '16 at 18:07

3 Answers3

3

createDrinkOrder1 returns a function that you can LATER call:

var orderFunction = createDrinkOrder1(passenger);
// nothing printed to console yet

// later:
orderFunction();
// now printed to console

createDrinkOrder2 immediatelly prints the results:

createDrinkOrder1(passenger);
// printed to console

You sometimes return a function not only to be able to call it later, but also to create a closure, that might be an interesting topic to read about.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • So actually, in the `orderFunction` one of the `console.log` is already returned and stored in the `orderFunction` so whenever it runs, it'll run the `console.log` instead of running the whole function again going through the `if/else` Is this right? – Tsuna Jan 29 '16 at 01:43
0

first class

function createDrinkOrder(passenger) {
    var orderFunction;
    if (passenger.ticket === "firstclass") {
        orderFunction = function() {
            console.log("Would you like a cocktail or wine?");
        };
    } else {
        orderFunction = function() {
            console.log("Your choice is cola or water.");
        };
    }
    return orderFunction;
}

Returns a function that you can use after its call resolves.

var a = createDrinkOrder(some_passenger);

// a now has a function that I can call whenever I want

// If I want to print the result every second I can use
setInterval(a, 1000);

// Or just call it immediately
a();

simple function

function createDrinkOrder2(passenger){
    if(passenger.ticket === "firstclass"){
        console.log("Would you like a cocktail or wine?");
    }else{
        console.log("Your choice is cola or water.");
    }   
}

Does not return anything and just prints immediately whenever you call createDrinkOrder2(some_passenger). That means that you must call createDrinkOrder2(some_passenger) if you want to print to console again.

zero298
  • 25,467
  • 10
  • 75
  • 100
0

First implementation will give the declared function to the calling function, so they could use it again if they wanted to, by calling variable 'orderFunction'. The second one would just print the value one time and cannot be reused, so depends what your use case is.

codebee
  • 824
  • 1
  • 9
  • 22