tags:

views:

463

answers:

6

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 class' mean in this context, as opposed to other objects?

EDIT (Jörg W Mittag): Exact Duplicate: "What is a first class programming construct?"

+12  A: 

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...

Sander Versluys
Quoting wikipedia is nice and dandy, but the description is written in a language for scientists and not for geeks. What the heck does all that mean anyway? The last sentence in that quote is vagu.
Spoike
@Spoike, true... provided javascript resource.
Sander Versluys
Conveniently a language that has first-class functions also has higher-order functions, as opposed to being limited to first-order functions, which would rule out first-class functions. (Though higher-order, not first-class is possible.)
TrayMan
I found nothing unclear in the Wikipedia quote, but the additional link is excellent.
ProfK
+1  A: 

It means that functions are objects, with a type and a behaviour. They can be dynamically built, passed around as any other object, and the fact that they can be called is part of their interface.

cadrian
+1  A: 

It means that function actually inherits from Object. So that you can pass it around and work with it like with any other object.

In c# however you need to refrain to delegates or reflection to play around with functions. (this got much better recently with lambda expressions)

Rashack
A: 

i guess when something is first class in a language, it means that it's supported by its syntax rather than a library or syntactic sugar. for example, classes in C are not first class

Jiang Qiu
+2  A: 

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.

Yuval A
But I can do this in C++: int twice(int x) { return x << 1; } int (*f)(int) = twice; std::cout<<(*f)(5)<<std::endl; Does that mean C++ treats functions as first class objects (with a funny syntax)?
Thomas L Holaday
Til you can create a function inside a function, i want to say no.
cHao
A: 

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.

Jörg W Mittag