View Full Version : Newbie question about functions + variables

07-03-2011, 11:53 PM

i know a little php, but new to javascript. much of the syntax is identical.

in reading the new rhino book (flanagan 6th), he gives the following example:

var square = function(x) {
return x*x;

okay, but then he does this:


as an example of "invoking two functions in one expresson".

(Prior he gives an example of defining a function with plus1, which is how I normally think of functions.)

uh, square is a *variable*, but he is using it as a FUNCTION.

Can somebody help me understand how this can be? how it works?

I'm having trouble conceptualizng how we use a variable as a function.

07-04-2011, 12:28 AM
The way square is defined is the same as

function square(x) {
return x * x;
}and so square is a function as well.

In your example

square(plus1(y))The plus1() function is evaluated first with y as input. plus1()then returns a value which is then used as input to the square() function.