Please help me understand this simple function

R

RC

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!
 
A

António Marques

RC said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top