...

View Full Version : What does the underscore do?



elcaro2k
10-20-2003, 04:11 PM
I am having trouble following this code. Can someone tell me what the underscore in front of the _compare function means. I can't figure out where this is being called from???
---------------
function compareByColumn(nCol, bDescending) {
var c = nCol;
var d = bDescending;

function _compare(n1, n2) {
var v;
var a = isNaN(getInnerText(n1.cells[c]))? getInnerText(n1.cells[c]):parseInt(getInnerText(n1.cells[c]));
var b = isNaN(getInnerText(n2.cells[c]))? getInnerText(n2.cells[c]):parseInt(getInnerText(n2.cells[c]));
if ( a < b)
v = (d) ? -1 : +1;
else if (a > b)
v = (d) ? +1 : -1;
else
v = 0;

return v;
}

return _compare;
}

elcaro2k
10-20-2003, 04:16 PM
Actually the error I am getting is "cells" is null or is not an object? Any ideas?

Roy Sinclair
10-20-2003, 05:02 PM
Underscore is a valid character for names in Javascript, it's not often used as the first character but it's valid to do so. The _compare function isn't being called in this function but the address of it is passed as a return value. Since it's a nested function I think that's an invalid usage. At least it doesn't follow the scope rules as I understand them.

Oh yeah, you can get rid of the smilies in your post by checking the checkbox in the Options box labeled: Disable Smilies in This Post.

liorean
10-20-2003, 05:09 PM
You just edit the post and make sure to check the "Disable Smilies in This Post" checkbox.

The _ is just part of the function name. You see, the function compareByColumn contains a local function (ie a function that can only be reached from within the compareByColumn function) by the name of _compare.

As for how the _compare function is called, I think that part is not included in the cut you've given us. You see, the return value of the compareByColumn function is a reference to the _compare function. This means that the call to the _compare function is done outside of the compareByColumn.


For example, if you store the results of a call to compareByColumn in the variable outside, what you really store in outside is a function reference. This reference can in turn be used to call the function like this outside(n1, n2); I think n1 and n2 are supposed to be tr elements in a table.

Search a bit for Higher Order Programming to learn more about the use of function references as return values of functions. It's really nifty, but sometimes confusing.

ScottInTexas
10-20-2003, 05:13 PM
The underscore is nothing more than the first character of the function name. The programmer could just as easily have named the function $MyFunction.

He has a function nested within a function. I'm not sure how it works because it generates an error in my editor. But the idea is the last return calls the function _compare.

liorean
10-20-2003, 05:16 PM
Originally posted by Roy Sinclair
The _compare function isn't being called in this function but the address of it is passed as a return value. Since it's a nested function I think that's an invalid usage. At least it doesn't follow the scope rules as I understand them.

It's a way of creating private variables. That's the marvel of it. You see, this function actually acts like a constructor for other functions very similar in function but not quite the same. In JavaScript, a variable exists as long as it's referenced by anything, and the variables c and b in the compareByColumn function is referenced by it's own child function, _compare. Thus, they are available from just that function reference, while both the variables in each specific case and the _compare function are unavalable except from the function reference.

elcaro2k
10-20-2003, 05:18 PM
You are right liorean. It is confusing. Thanks for the help!

elcaro2k
10-20-2003, 05:24 PM
As for how the _compare function is called, I think that part is not included in the cut you've given us.

I am attaching the whole script. Can you tell me where the _compare is being called?

liorean
10-20-2003, 05:38 PM
Roy: Have a look at <http://www.crockford.com/#javascript>. It explains things much better than I could.

elcaro: I can understand you didn't find it. It's well hidden behind some of JavaScript's most obscure (but very powerful and useful) features. In this case, the sort method of arrays takes a function reference as it's single argument. This function must be of the nature that it returns a -1,0,1 value depending on whether one element in the array should sort before or after another value. The call to the _compare function is thus done by the sort method and does not actually appear in the script. The sort method is called in line 15, with the argument being the return value of the compareByColumns function, in other words a function reference to _compare. Confused enough?

elcaro2k
10-20-2003, 05:51 PM
Yikes!!! I feel like I am back in Logic and Language 235.

I think I can figure it out from here. This is one that I don't feel bad about asking for help on.

Thanks Everyone!!!
Ray



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum