Are functions true?

I just started reading JavaScript: the final guide, and I don’t understand what the author means when he says:

"The most important thing about JavaScript functions 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?

+6
source share
3 answers

This means that you can store, read, and call functions like objects. Therefore, they call functions “first-class citizens”: there is no different relationship between data and functions (at least at the conceptual level, obviously, the runtime can implement it differently). In most static typed object-oriented languages, such as older versions of Java, this is not possible (easy). You can pass functions as arguments, store functions in objects, and even (which makes JavaScript pretty special), print the implementation of the function.

Examples

Passing them as arguments:

you can pass the function as an argument to (for example) another function:

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

here f is a function.

Preservation:

You can save the function in a variable:

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

now you can call f(2) to get 4 .

print function :

you can get the signature and implementation of the self-implemented function using the .toString method. For example, using node :

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

(obviously, the above examples are pretty simple and don't make much sense, but imagine that f , for example, updates a text box on a web page or does a complex query ...). Hope you can appreciate the power of this.

Other programming languages

Especially with older versions of Java, you could not do this. For example, a code snippet, for example:

 //This is Java code to make an analogy public class Foo { public static int Bar (int x) { return x+2; } } 

You cannot save Foo.Bar into a variable, pass a function to another method ... Most (object-oriented) programming languages ​​made a clear distinction between data and functions. Obviously, there are pros and cons for processing data and functions the same or different, although looking at the evolution of programming languages, I would say that treating them the same way seems to be the direction in which the community is developing (obviously, not all, and this is only a personal expression).

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

+4
source

This means that JavaScript does not distinguish between functions or strings, or numbers or boolean values, as important as they are all the same.

These are all objects.

Objects are key / value pairs in JS, like

 "neilmunro".length; 

Will be 9.

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

Print "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!"); }); 

This is a bit of a bend of the mind, and there is much more than just that, but this is the main idea.

+6
source

I believe this means that you can assign or pass a function in the same way 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 

All of them can be assigned to a variable or passed to a function, regardless of whether it is a string, function, or object. All of them are "true values."

+1
source

All Articles