In a recent question, I received suggestions to talk on, amongst other things, the aspect of JavaScript where functions are \'first class\' objects. What does the \'first c
More complete approval of Strachey-Sussman-Abelson's formulation. So if your language supports such a construct then you've got a function as a first-class language :)
var men = function (objectOfAdmiration) {
return objectOfAdmiration();
};
men.isSweetHeart = true;
var women = function (objectOfAdmiration) {
return objectOfAdmiration();
};
women.isSweetHeart = true;
var aliens = function (objectOfAdmiration) {
return objectOfAdmiration();
};
function like(obj){
if (obj.isSweetHeart) {
return function (){ return "Holy TRUE!"};
}
else {
return function (){ return "Holy CRAP!"};
}
}
alert("Men like women is " + men(like(women))); // -> "Holly TRUE!"
alert("Women like men is " + women(like(men))); // -> "Holly TRUE!"
alert("Men like aliens is " + men(like(aliens))); // -> "Holly CRAP!"
alert("Aliens like women is " + aliens(like(women))); // -> "Holly TRUE!" :)
//women(like(aliens)); // Who knows? Life is sometimes so unpredictable... :)
In short, anything is a first-class object if it acts in the language as a state manipulation sort of object or type of object. Simply something you can operate on and pass around statements and evaluate in expressions at the same time. Or even shorter: when you can think of a function as an object that can be additionally invoked.