Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is meant by 'first class object'?

Tags:

javascript

People also ask

What is meant by first-class objects in Python?

In Python, functions behave like any other object, such as an int or a list. That means that you can use functions as arguments to other functions, store functions as dictionary values, or return a function from another function. This leads to many powerful ways to use functions.

Which is a first-class object in Java?

Java strings are first-class objects. Strings as objects provides several advantages to the programmer: the manner in which you obtain strings and elements of strings is consistent across all strings and all systems.

What is meant by first-class objects in the context of programming languages?

A first-class object is an entity within a programming language that can: Appear in an expression. Be assigned to a variable. Be used as an argument. Be returned by a function call.

What is meant by class object?

In computer programming, the object class refers to a class created to group various objects which are instances of that class. Classes are code templates for creating objects. In cases where objects need to be grouped in a certain way, an object class is the "container" for a set of objects built on these templates.


To quote Wikipedia:

In computer science, a programming language is said to support first-class functions (or function literal) if it treats functions as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions.

This page also illustrates it beautifully:

Really, just like any other variable

  • A function is an instance of the Object type
  • A function can have properties and has a link back to its constructor method
  • You can store the function in a variable
  • You can pass the function as a parameter to another function
  • You can return the function from a function

also read TrayMan's comment, interesting...


The notion of "first-class functions" in a programming language was introduced by British computer scientist Christopher Strachey in the 1960s. The most famous formulation of this principle is probably in Structure and Interpretation of Computer Programs by Gerald Jay Sussman and Harry Abelson:

  • They may be named by variables.
  • They may be passed as arguments to procedures.
  • They may be returned as the results of procedures.
  • They may be included in data structures.

Basically, it means that you can do with functions everything that you can do with all other elements in the programming language. So, in the case of JavaScript, it means that everything you can do with an Integer, a String, an Array or any other kind of Object, you can also do with functions.


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.


In javascript functions are first class objects because it can do a lot more than what objects can do.

  • A function is an instance of an Object type.

Function instanceof Object //returns true

Like an object a function can have properties and can have a link back to it’s constructor function.

 var o = {}; // empty object 'o'
    o.a = 1 ; 
    o.b = 2 ; 

    console.log(o.a); // 1
    console.log(o.b); // 2 


    function foo(){};
    foo.a = 3 ; 
    foo.b = 4 ; 

    console.log(foo.a);  // logs 3
    console.log(foo.b);  // logs 4 
  • Functions can be stored in a variable as a value.

  var foo = function(){}; 
    console.log(foo); // function(){}
  • Functions can be passed as arguments to other functions

function callback (foo){
      foo();
}

callback(function(){console.log('Successfuly invoked as an argument inside function callback')})
  • You can return a function from a function

 function foo(){
	    return function(){console.log('working!')};
    }

    var bar = foo();
    bar(); // working!
  • Can be stored in a variable as a reference.

    var sum = function (a,b){return a+b}  
    sum(4,4);

JavaScript functions are first-class functions meaning functions and objects are treated as the same thing. Functions can be stored as a variable inside an object or an array as well as it can be passed as an argument or be returned by another function. That makes function "first-class citizens in JavaScript"

JavaScript uses literal notation syntax which makes it hard to fully grasp the fact that in JavaScript functions are objects.

For example..

var youObj1 = new Object();
// or
var youObj1 = {};

both declerations are equivalent. By using new we are calling the constructor function of an Object. Also by using {} (JavaScript shortcut called literals) we are calling the construction function of an Object. {} is just a shorter representation for instantiating the constructor.

Most languages uses new keyword to create an object, so lets create a JavaScript object.

var myFunction = new Function("a",  "b", 'return a_b');

As you see we created an object name function.

Creating same object name function using JavaScript function expression..

var myFunction = function myFunction(a,b) {
    return a+b;
}

Here we go we just created a object name function.


Simple test. If you can do this in your language (Python as example):

def double(x):
    return x*x

f = double

print f(5) #prints 25

Your language is treating functions as first class objects.