Functions Returning Functions - Javascript Function

Javascript examples for Function:Function Return

Description

Functions Returning Functions

Demo Code


function interviewQuestion(job) {
  if (job === "designer") {
    return function(name) {
      console.log(name + ", can you please explain what UX design is?");
    }//from  w  w  w  .jav a  2s .c  om
  }
  else if (job === "teacher") {
    return function(name) {
      console.log("What subject do you teach, " + name + "?");
    }
  }
  else if (job === "special forces operator") {
    return function(name) {
      console.log(name + ", how many confirmed kills do you have?");
    }
  }
  else {
    return function(name) {
      console.log("Hello, " + name + ". What do you do?");
    }
  }
}

var teacherQuestion = interviewQuestion("teacher");
teacherQuestion("John");


var designerQuestion = interviewQuestion("designer");
designerQuestion("Kate");
designerQuestion("Mark");
designerQuestion("Quentin");
designerQuestion("Liz");

var specialForcesQuestion = interviewQuestion("special forces operator");
specialForcesQuestion("Jack Bauer");

interviewQuestion("special forces operator")("Jack Bauer");

Related Tutorials