LaughingMan
LaughingMan

Reputation: 63

Functions are true values?

I've just started reading JavaScript: The Definitive Guide and I don't understand what the author means when he says:

"The most important thing about functions in JavaScript is that they are true values and that JavaScript programs can treat them like regular objects."

What does he mean that they are "true values"? And why does this mean that they can be treated like objects?

Upvotes: 6

Views: 224

Answers (3)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476709

It means that you can store, read, and call functions like objects. Therefore they call functions "first class citizens": there is no different treatment between data and functions (at least not on the conceptual level, evidently the runtime environment can implement it differently). In most static typed object-oriented languages like the old versions of Java, this could not be done (easily). You can pass functions as arguments, save functions in objects, and even (which makes JavaScript rather special), print the implementation of a function.

Examples

Passing them as arguments:

you can pass a function as an argument to (for instance) another function:

function foo (f,x) {
    return f(x); //calling f, thus f is a parameter and can be called
}

here f is that function.

Storing them:

You can store a function in a variable:

var f = function (x) { return x+2 };

now you can call f(2) in order to receive 4.

printing functions:

you can obtain the signature and implementation of a self-implemented function with the .toString method. For instance with node:

> console.log(f.toString());
function (x) { return x+2 }

(evidently the examples listed above are rather simple and don't make much sense, but imagine that f for instance will update a text field on a webpage, or will perform a complex query,...). I hope you can appreciate the power of this.

Other programming languages

Especially with the old versions of Java, you could not do that. For instance a piece of code like:

//This is Java code to make an analogy
public class Foo {

    public static int Bar (int x) {
        return x+2;
    }

}

You could not store Foo.Bar into a variable, pass the function to another method,... Most (object-oriented) programming languages once made a clear distinction between data and functions. Evidently there are pros and cons for treating data and functions the same or different, although by looking at the evolution of programming languages, I would say treating them the same seems to be the direction in which the community evolves (evidently not everyone, and this is only a personal statement).

Programming languages that definitely see functions as first class citizens are functional programming languages like Haskell where there are - conceptually speaking - no other kind of objects than functions.

Upvotes: 5

Magnus Engdal
Magnus Engdal

Reputation: 5634

I believe it means you can assign or pass a function in the same way as you can with any other object, integer, string or something else.

For instance

var foo = 1; // integer
var foo = 'bar'; // string
var foo = false; // boolean
var foo = {value: 'bar'}; // object
var foo = function bar() {}; // function

They can all be assigned to a variable or passed to a function, no matter if it's a string, a function or an object. They are all "true values".

Upvotes: 1

NMunro
NMunro

Reputation: 910

It means JavaScript does not make a distinction between functions or strings, or numbers or booleans, as far as it is concerned they're all the same.

They're all objects.

Objects are key/value pairs in JS, just like

"neilmunro".length;

Will be 9.

function sayHi() { console.log("Hi"); }
sayHi.name;

Will print out "sayHi".

Functions can be passed to other functions:

function doSomething(name, func) {
    console.log("Hi: " + name);
    func();
}

doSomething("Neil", function() { console.log("You're a human!"); });

It's a bit mind bending, and there's a lot more to it than just that, but it's the basic idea.

Upvotes: 6

Related Questions