Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post

    Newbie question about functions + variables

    hi.

    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:

    Code:
    var square = function(x) {
       return x*x;
    };
    okay, but then he does this:

    Code:
    square(plus1(y))
    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.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    The way square is defined is the same as

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

    In your example

    Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •