What does it mean: "functions are first class objects in Javascript"?

Asked

Viewed 2,363 times

26

Several times I have heard this: "functions are first class objects in Javascript". Functions being stored in variables and passed in methods and so on was not something that surprised me so much in Javascript initially, as I was already used to C#delegates. My question is about this term "first class," which exactly means this?

3 answers

20


The term "first class" means that the function is treated in the same way as any type of value in javascript. For example, for you to assign a value to a variable, you do so:

var meuInteiro = 10;  
var minhaString = "minha string";
var minhaFuncao = function() { console.log('minha função'); }

That is, you used the same syntax in all three forms, and javascript understands it in the same way. You can also pass the function as parameters, object attributes, etc., just like you do with any other type. That’s what "first class means".

-- A great article on the subject: http://javascriptbrasil.com/2012/09/05/funcoes-de-primeira-classe/

  • 4

    To complete: one function can return another function.

6

In languages like Java, C# and Visual Basic, you program everything in classes - they are the "citizens" of first class. The method Main that starts your program is a member of a class. The objects you encode are all instances of classes someone wrote. Open your IDE and see... In C#code, the only things you write outside the scope of classes are decoration (for the classes themselves), declaration of new namespaces (namespaces) and declaration of which namespaces will be used.

In Javascript there are no classes - at least not in the same way as in other languages. The typification is completely different. There are some basic types (number, date, string, boolean and array, if I remember correctly) and the guy object. But they’re not classes. You have, so to speak, classless objects.

And here the cat jump: Javascript types are functions. If you want to create a new type, you declare a function. It becomes a constructor of itself, and all the members you assign directly to it are static. To give instance members to your type, you associate them with the prototype (prototype) of function.

Since every type you write on your own in Javascript will necessarily be a function, we say that in Javascript, the functions are the objects (or "citizens" as defined in) first-class.

  • 2

    There is nothing subjective or abstract in the statement. First class function is a well-defined and well-known concept, and has nothing to do with object-oriented programming.

  • @Pedrorodrigues I didn’t know that definition. Thank you! I’m adjusting the text.

1

In English first-class functions, but it cannot be confused and preconceptualize that there are functions of first, second, third class, etc. Nor are they special functions, differentiated from others.

In Javascript and other modern languages the functions are first-class, i.e., they are objects that have properties and methods and can be passed as arguments, assigned to variables or returned as any other object (http://pt.wikipedia.org/wiki/JavaScript)

Luciano Ramalho also speaks very well on the concept.

  • And what is not "first-class" is what? What are the examples?

  • 1

    It’s like Luciano Ramalho says in his text. All functions are first-class, there are no second-class functions (there are methods, which are also first-class objects in these languages). A related term is "higher-order functions" (Higher order functions). Here we are talking about a special category of functions: they are functions that receive other functions as arguments, and/or return functions as a result. In Javascript, various array methods such as map, filter, foreach and reduce are examples of higher-order functions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.