Hi
I have decided it is time to learn Javascript (I know PHP fairly well, so
I thought it shouldn't be too difficult!!!)
I have a couple of question though, which I can highlight with the
following code...
---
function mysortfn(a,b) {
document.write('Comparing '+a+'and '+b);
document.write('<br />');
if (a<b) return -1;
if (a>b) return 1;
return 0;
}
.... get called by;
myArray = [4,2,6,5,3,1];
document.write(myArray);
myArray.sort(mysortfn);
document.write(myArray);
---
The result is what you would expect - the unsorted array, the
'myArray.sort' and then the sorted array.
My question are;
1. How come document.write calls the 'mysortfn' without parentheses and
works?
2. Why, if I call it WITH parentheses, does it return 'undefined' - i.e.
myArray.sort(mysortfn());
3. How come the 'myArray.sort(mysortfn);' code prints out to the page
anyway? It hasn't been told to by 'document.write()'...
4. How does 'mysortfn(a,b)' retrieve values for a & b? They aren't passed.
Help really appreciated
Steve 4 5274
Steve wrote: Hi
I have decided it is time to learn Javascript (I know PHP fairly well, so I thought it shouldn't be too difficult!!!)
I have a couple of question though, which I can highlight with the following code...
--- function mysortfn(a,b) { document.write('Comparing '+a+'and '+b); document.write('<br />'); if (a<b) return -1; if (a>b) return 1; return 0; }
... get called by;
myArray = [4,2,6,5,3,1];
document.write(myArray);
myArray.sort(mysortfn);
document.write(myArray);
---
The result is what you would expect - the unsorted array, the 'myArray.sort' and then the sorted array.
My question are;
1. How come document.write calls the 'mysortfn' without parentheses and works?
I do not see anywhere in your code where within the document.write your
function 'mysortfn' is being called.
2. Why, if I call it WITH parentheses, does it return 'undefined' - i.e. myArray.sort(mysortfn());
Array.sort has an optional argument which expects a function. What you
are doing here is actually making a function call. And the return value
is being sent in as a parameter to Array.sort.
3. How come the 'myArray.sort(mysortfn);' code prints out to the page anyway? It hasn't been told to by 'document.write()'...
myArray.sort(mysortfn) is not the one doing the document.write(). Your
function, mysortfn, is the one doing the document.write().
4. How does 'mysortfn(a,b)' retrieve values for a & b? They aren't passed.
Array.sort(), when given a comparison function, will supply the values
for your function. The comparison function (mysortfn), should take two
arguments, a and b, and should return one of the following:
A value less than zero for a < b.
Zero, if a and b are equivalent.
A value greater than zero for a > b.
Help really appreciated
Steve
Steve wrote: Hi
I have decided it is time to learn Javascript (I know PHP fairly well, so I thought it shouldn't be too difficult!!!)
I have a couple of question though, which I can highlight with the following code...
--- function mysortfn(a,b) { document.write('Comparing '+a+'and '+b); document.write('<br />'); if (a<b) return -1; if (a>b) return 1; return 0; }
... get called by;
myArray = [4,2,6,5,3,1];
document.write(myArray);
myArray.sort(mysortfn);
document.write(myArray);
---
The result is what you would expect - the unsorted array, the 'myArray.sort' and then the sorted array.
My question are;
1. How come document.write calls the 'mysortfn' without parentheses and works?
document.write doesn't call mysortfn() at all, the sort() method of your
myArray array object does, though indirectly.
You have given sort() a function *reference* as a parameter, so sort
uses that function to do the sort. mysortfn() is using document.write,
not the other way around. 2. Why, if I call it WITH parentheses, does it return 'undefined' - i.e. myArray.sort(mysortfn());
When you use a function name without (), you are passing a *reference*
to the function, you are saying 'use this function later'. If you
follow the reference with (), you are passing the *result* of running
the function. i.e. you are saying 'use the result of running this
function as the parameter'.
mysortfn() doesn't return anything useful to sort(), so it (sort) barfs. 3. How come the 'myArray.sort(mysortfn);' code prints out to the page anyway? It hasn't been told to by 'document.write()'...
Because mysortfn is being called by sort when sorting the array. And
mysortfn uses document.write. 4. How does 'mysortfn(a,b)' retrieve values for a & b? They aren't passed.
Read the ECMAScript Language Specification, section 15.4.4.11, it is all
explained there.
In short, you can use something like myArray.sort(compareFn). If
compareFn (in your case it's mysortfn) is defined, it should take two
arguments x and y and return a negative value if x<y, zero if x=y or a
positive value if x>y.
If compareFn is not undefined and is not a consistent comparison
function (e.g. you use mysortfn(), or the result of running mysortfn)
the what happens is implementation-defined. In this case, it results in
/undefined/. Help really appreciated
I hope it did. :-)
--
Rob
On Tue, 27 Sep 2005 23:26:01 +0000, RobG wrote: Steve wrote: Hi
I have decided it is time to learn Javascript (I know PHP fairly well, so I thought it shouldn't be too difficult!!!)
I have a couple of question though, which I can highlight with the following code...
--- function mysortfn(a,b) { document.write('Comparing '+a+'and '+b); document.write('<br />'); if (a<b) return -1; if (a>b) return 1; return 0; }
... get called by;
myArray = [4,2,6,5,3,1];
document.write(myArray);
myArray.sort(mysortfn);
document.write(myArray);
---
The result is what you would expect - the unsorted array, the 'myArray.sort' and then the sorted array.
My question are;
1. How come document.write calls the 'mysortfn' without parentheses and works?
document.write doesn't call mysortfn() at all, the sort() method of your myArray array object does, though indirectly.
You have given sort() a function *reference* as a parameter, so sort uses that function to do the sort. mysortfn() is using document.write, not the other way around.
2. Why, if I call it WITH parentheses, does it return 'undefined' - i.e. myArray.sort(mysortfn());
When you use a function name without (), you are passing a *reference* to the function, you are saying 'use this function later'. If you follow the reference with (), you are passing the *result* of running the function. i.e. you are saying 'use the result of running this function as the parameter'.
mysortfn() doesn't return anything useful to sort(), so it (sort) barfs.
3. How come the 'myArray.sort(mysortfn);' code prints out to the page anyway? It hasn't been told to by 'document.write()'...
Because mysortfn is being called by sort when sorting the array. And mysortfn uses document.write.
4. How does 'mysortfn(a,b)' retrieve values for a & b? They aren't passed.
Read the ECMAScript Language Specification, section 15.4.4.11, it is all explained there.
In short, you can use something like myArray.sort(compareFn). If compareFn (in your case it's mysortfn) is defined, it should take two arguments x and y and return a negative value if x<y, zero if x=y or a positive value if x>y.
If compareFn is not undefined and is not a consistent comparison function (e.g. you use mysortfn(), or the result of running mysortfn) the what happens is implementation-defined. In this case, it results in /undefined/.
Help really appreciated
I hope it did. :-)
You did - a big help. Thank you very much. Now on with the learning... :-)
Steve said: Hi
I have decided it is time to learn Javascript (I know PHP fairly well, so I thought it shouldn't be too difficult!!!)
I have a couple of question though, which I can highlight with the following code...
--- function mysortfn(a,b) { document.write('Comparing '+a+'and '+b); document.write('<br />'); if (a<b) return -1; if (a>b) return 1; return 0; }
... get called by;
myArray = [4,2,6,5,3,1];
document.write(myArray);
myArray.sort(mysortfn);
document.write(myArray);
---
The result is what you would expect - the unsorted array, the 'myArray.sort' and then the sorted array.
My question are
You need to read up on what the sort() method of Arrays does.
The argument that you pass to it is a reference to a function.
Within the sort() method, that function is called several times
with two arguments passed to it each time. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Daniel Hansen |
last post: by
|
11 posts
views
Thread by Kamilche |
last post: by
|
1 post
views
Thread by Joel Thornton |
last post: by
|
8 posts
views
Thread by Andreas Lagemann |
last post: by
|
6 posts
views
Thread by lovecreatesbeauty |
last post: by
|
9 posts
views
Thread by kernelxu |
last post: by
|
5 posts
views
Thread by Stef Mientki |
last post: by
| |
6 posts
views
Thread by SanPy |
last post: by
| | | | | | | | | | |