Please help me understand this simple function

Discussion in 'Javascript' started by RC, Jun 7, 2006.

  1. RC

    RC Guest

    http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2

    You can see above link or read below i copy/paste from above link

    <script type="text/javascript">

    function sortNumber(a, b)
    {
    return a - b
    }

    var arr = new Array(6)
    arr[0] = "10"
    arr[1] = "5"
    arr[2] = "40"
    arr[3] = "5"
    arr[4] = "1000"
    arr[5] = "1"

    document.write(arr + "<br />")
    document.write(arr.sort(sortNumber))

    </script>

    Please help me to understand the last line:

    arr.sort(sortNumber)

    The function sortNumber has TWO parameters a and b pass in.
    I just can't understand how the array 0 to 5 pass thire
    values into there to sort.

    Please explain to me. Thank Q!
     
    RC, Jun 7, 2006
    #1
    1. Advertising

  2. RC wrote:

    > function sortNumber(a, b)
    > {
    > return a - b
    > }
    >
    > var arr = new Array(6)
    > arr[0] = "10"
    > arr[1] = "5"
    > arr[2] = "40"
    > arr[3] = "5"
    > arr[4] = "1000"
    > arr[5] = "1"
    >
    > document.write(arr + "<br />")
    > document.write(arr.sort(sortNumber))
    >
    > Please help me to understand the last line:
    >
    > arr.sort(sortNumber)
    >
    > The function sortNumber has TWO parameters a and b pass in.
    > I just can't understand how the array 0 to 5 pass thire
    > values into there to sort.


    In arr.sort(sortNumber), 'sortNumber' is not a function call but just a
    reference to the function that had been defined above. The sort()
    function will then use the function provided (sortNumber) anytime it
    needs to compare two elements (to sort an array, you need to have a way
    to compare any two of its elements).

    Most of the power of javascript is in this ability to use functions as
    real objects, which makes it a very different language from the whole
    Algol family, from C to java (and much more pleasant to work with).

    To illustrate, take the following:

    <script type="text/javascript">

    function addTo(anObject)
    {
    anObject.field='here!';
    return anObject;
    }

    function example(anObject, aFunction)
    {
    aFunction(anObject);
    }

    function exampleReturn()
    {
    return arguments[1](arguments[0]);
    }

    var o1={};
    example(o1, addTo);
    document.write(o1.field+'<br>'); // 'here!'

    var o2={};
    document.write(exampleReturn(o2, addTo).field+'<br>'); // 'here!'

    </script>

    Notice, in the second example, that 'return' works normally, returning
    the value of the function call. In the first example, function 'example'
    just ignores the value it receives from the call, returning itself
    'undefined'.

    On a completely unrelated note, notice that 'exampleReturn' doesn't
    define its arguments. Instead, they are accessed through the 'arguments'
    object. In practice, when you define the arguments of a function, you're
    just giving aliases to the elements of 'arguments'. Among other things,
    this allows a function to have a variable number of arguments, even if
    they are named:


    <script type="text/javascript">

    function sumTo(anObject)
    {
    // Same as 'if(!anObject) anObject={};'
    anObject = anObject || {};
    anObject.sum=0;
    for(var i=1; i<arguments.length; ++i)
    anObject.sum += arguments;
    return anObject;
    }

    var s1={otherField: 'otherValue'};
    sumTo(s1, 1, 2, 3, 4);
    document.write(s1.sum+'<br>'); // '10'
    document.write(s1.otherField+'<br>'); // 'otherValue'

    var s2=sumTo(null, 2, 6);
    document.write(s2.sum+'<br>'); // '8'
    document.write(s2.otherField+'<br>'); // 'undefined'

    var s3=sumTo();
    document.write(s3.sum+'<br>'); // '0'

    </script>

    In the first case, a variable is created, with an initialised field, and
    sumTo is called with 4 numbers. They are summed, put in a new 'sum'
    field (if there had beena previous one, it would have been overwritten),
    and the object is returned.

    In the second case, no object is passed to sumTo, only a list of
    numbers, so it creates a new object. As there was no 'otherField' to
    begin with, writing it to output yields 'undefined'.

    The third case is a variant of the second in which nor arguments are
    supplied at all, so the function just builds the object, initalises the
    sum. the loop stops before it even begins, and the object is returned.

    Notice the use of anObject with boolean operators (!, ||, etc).
    Javascript typecasts null, undefined, "" and 0 to false.
    This can be both handy and tricky.
    --
    am

    laurus : rhodophyta : brezoneg : smalltalk : stargate
     
    António Marques, Jun 7, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    462
    Joerg Jooss
    Aug 31, 2005
  2. Replies:
    12
    Views:
    625
    Virgil Green
    Aug 29, 2005
  3. HalcyonWild

    Please help me understand EJB

    HalcyonWild, Sep 29, 2005, in forum: Java
    Replies:
    4
    Views:
    506
    Roedy Green
    Sep 30, 2005
  4. KK
    Replies:
    2
    Views:
    626
    Big Brian
    Oct 14, 2003
  5. Fily Salas
    Replies:
    4
    Views:
    262
    Fily Salas
    Apr 28, 2011
Loading...

Share This Page