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 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What does the underscore do?

    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;
    }
    Last edited by elcaro2k; 10-20-2003 at 04:09 PM.

  • #2
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually the error I am getting is "cells" is null or is not an object? Any ideas?
    Last edited by elcaro2k; 10-20-2003 at 04:15 PM.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 10-20-2003 at 04:19 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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.
    Scott Stewart
    Always happy to learn from pros.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are right liorean. It is confusing. Thanks for the help!

  • #8
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Attached Files Attached Files

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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?
    Last edited by liorean; 10-20-2003 at 04:40 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    New Coder
    Join Date
    Aug 2002
    Posts
    89
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

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