Wednesday, September 15, 2021

In javascript how a function can return another function?

 In javascript function is object .So function can return another function .Example is below-

function interviewQuestion(job) {

    if (job === "engineer") {

        return function (name) { //annonymous function

            console.log(name + " can you please explain what is engineering");

        }

    } else if (job === "designer") {

        return function (name) {

            console.log(name + " how to design good UX");

        }

    } else {

        return function (name) {

            console.log(name + " don't be unemployed");

        }

    }

}

var question = interviewQuestion("engineer");// var question store the function expression .then                                                                                  called it.

question("lokman");

No comments:

Post a Comment

Abstract factory pattern

When single task can be done by multiple groups/family of objects and decision is taken at the runtime.