SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What does $(which); do???!??!?

    I'm reading the Jquery Novice to Ninja book and on page 268 where there is this unexplained snippet of code containing a selector $(which);

    I do not know what this means. Reading this book is FRUSTRATING as I have to keep a laundry list of things I got to research when something is not explained, but the string $(which); is NOT searchable as it seems to be mis-interpreted so I have to ask here. What does this piece of code mean???!?!??

    Ok, on fourth read, I note that the text says its's storing our what the user has started to drag, but I would like to see some further explanation about what is the difference between this and $(which) and how do I know which of these I'm supposed to use?

    The over all effect of having foreign syntax used without explanation is like taking a foreign language course where words are used without defining them in sentences and you have to guess what the word means and of course like the Indiana Jones movie where they have to chose the right cup out of many, the chances of a wrong guess and needed unlearning are great!
    Last edited by GarryFre; Jun 9, 2010 at 14:16. Reason: My grammar errors are as the sands of the sea.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    "which" is probably defined elsewhere as a selector string, like '#something > p:first-child', and that's what is being passed to the $ funciton.

    The only certainty is that "which" is a variable which was defined elsewhere.

    It's hard to say without the context what's going on.

  3. #3
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    In the example, "which" is just a javascript var. It certainly would not be my choice to use as an example.

    For example, it looks like it's used in this manner:

    Code:
    var which = $('#someid div p.someclass');
    
    $(which).fadeOut(200);
    Look on page 266 and at the function puffRemove(). puffRemove() is getting "ui.draggable" passed into the "which" variable (the contents of the puffRemove() function are on page 268)
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Looks like it's redundant anyway. If which is a jQuery object already (through using $), they could just have done this:

    Code:
    which.fadeOut(200);
    unless in between those lines "which" is turned into the actual DOM element (using get()), in which case it would have to be turned back into a jQuery object using $.

  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    In the example, "which" is just a javascript var. It certainly would not be my choice to use as an example.

    For example, it looks like it's used in this manner:

    Code:
    var which = $('#someid div p.someclass');
    
    $(which).fadeOut(200);
    Look on page 266 and at the function puffRemove(). puffRemove() is getting "ui.draggable" passed into the "which" variable (the contents of the puffRemove() function are on page 268)
    Thanks kindly, just like the others you were correct, but you found the exact place where which was declared. I am like another here, unimpressed with the choice of variable names here. I would have used something like selectedElement or selectedItem which would be far more easily recognized as a variable name rather than some form of control statement keyword as some languages use that word for in syntax like switch and case statements.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by GarryFre View Post
    I am like another here, unimpressed with the choice of variable names here. I would have used something like selectedElement or selectedItem which would be far more easily recognized as a variable name rather than some form of control statement keyword as some languages use that word for in syntax like switch and case statements.
    I tend to use el for elements, and when it's a jquery object I prefix the variable with dollar, so it would be $el for jquery reference to an unknown element. That way when you come back to the code some 6 months later, you instantly know that $el is supposed to be a jquery reference to an element.

    That's the most generic I would go with that type of variable name. There's always room to make the variable name more explicit, where that makes the code easier to understand.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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