using the power of closures - Javascript Function

Javascript examples for Function:Closure

Description

using the power of closures

Demo Code

function interviewQuestion(job) {
  return function(name){
    if (job === "designer") {
      console.log(name + ", can you please explain what UX design is?");
    }//from   www.  j a  v a 2  s .  c  o  m
    else if (job === "teacher") {
      console.log("What subject do you teach, " + name + "?");
    }
    else if (job === "special forces operator") {
      console.log(name + ", how many confirmed kills do you have?");
    }
    else {
      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("primal scream therapist")("Smacky");

Related Tutorials