Tuesday, September 14, 2021

Example of function taking function as arguments.

 var year=[1990,1965,1970,1980];

function arrayCalc(arr,fn){

    var res=[];

    for(var i=0;i<arr.length;i++){

        res.push(fn(arr[i]))

    }

    return res;

}

function calculateAge(el){

    return 2021-el;

}

var result=arrayCalc(year,calculateAge);//here calculateAge is the callback function .

console.log(result);


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.