functions are first class objects

by peterbenoit

JavaScript

function feedCat(){
    console.log("feeding the cat");
}
console.log(feedCat instanceof Object);  // function is an instance of object type
    
    
//A function can have properties and has a link back to its constructor method    
feedCat.food = "kibble";
console.log(feedCat.food);
console.log(feedCat.constructor);

//You can store the function in a variable:
var eveningChore = feedCat;eveningChore();

//And then pass it as a param to another function
function doEveningChores(chores){
    for(var x=0; x<chores .length; x++)
        chores[x]();
}
doEveningChores([feedCat]);

//Return the function of a function:
function tonightChores(){
    return feedCat;
}
var tonight = tonightChores();
tonight();