JavaScript: Function Factory Example

function greeting(myGreeting){
  return function(whoToGreet){
    return myGreeting + ' ' + whoToGreet;
  };
}
var greetingsFromShane = greeting('Hi this is Shane. Welcome');
var greetingsFromChiayi = greeting('Hi this is Chiayi. Nice to meet you');

greetingsFromChiayi('Nick');
<"Hi this is Chiayi. Nice to meet you Nick"

greetingsFromShane('Ting-hui');
<"Hi this is Shane. Welcome Tinghui"

Leave a Reply