Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialammarkhan
Front End Web Development Techdegree Student 21,661 PointsHow did variable became function?
In the video example in the end and in the video, the `var counter1
was a var but later on, it was used as a function counter1()
, how and is this a part of closures?
3 Answers
Steven Parker
231,248 PointsIn JavaScript, a variable can hold a a value or a function.
This concept is known as "first class objects". For example, each of these statements creates a function named "hey":
function hey() { console.log("hey!"); }
var hey = function () { console.log("hey!"); }
var hey = () => console.log("hey!");
In the video, counter1 was assigned using a function which returned another function when called.
Neveen Atik
3,329 PointsAs I understand functions are values so It means that you can store, read, and call functions like objects. Therefore they call functions "first class citizens". You can pass functions as arguments, save functions in objects, and even print the implementation of a function. here is more information I read on stackoverflow I hope this makes things clear:
answered by Willem Van Onsem https://stackoverflow.com/questions/33920286/functions-are-true-values
//Example of storing a function in a variable
var f = function (x) {
return x+2
};
now you can call f(2) and it will return 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.
ammarkhan
Front End Web Development Techdegree Student 21,661 PointsSteven Parker I didn't downvote, I didn't understand your example though.
Steven Parker
231,248 PointsThe last 2 examples show a variable being assigned with a function, which causes the variable to "become" a function as the question was asking.
nico dev
20,364 Pointsnico dev
20,364 PointsWhat's with the downvotes? Scratching my head...
This answer was perfect and clear. Just saying...