SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple jQuery question from a noob with the brain of a chimpanzee

    I am reading a book on jQuery and I've seen in a few code examples in the book where the author will declare variables with a preceding $. My question is simply whether or not the $ is significant in itself or is it simply a convention/tradition to broadcast immediately to anyone who sees the variable that's its a jQuery object and 'not' a regular JavaScript object?

    If I declare two variable like this using jQuery:
    $jQueryvar = $('body');
    jQueryvar = $("body");

    Is there any difference between the two other than the $ being a part of the identifier. If there is a difference is it even possible to assign a jQuery object to a regular JavaScript object without using the preceding $ character?

    I think I've got my question across. It seems difficult to word for some reason but if anyone can help clear this up that would be great. Thanks.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Pre-pending a variable with $ basically allows us to see what is a jQuery object and whats a standard JavaScript variable, developers including myself do this now as it makes it a lot faster to debug code that mixes between the jQuery scope and the standard element scope.

    Overall there is no difference between the two apart from what they mean to us as developers.

  3. #3
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    Pre-pending a variable with $ basically allows us to see what is a jQuery object and whats a standard JavaScript variable, developers including myself do this now as it makes it a lot faster to debug code that mixes between the jQuery scope and the standard element scope.

    Overall there is no difference between the two apart from what they mean to us as developers.
    That was what I was hoping you would say. Every clever idea actually. I like it. No significance to the behavior of the script but instantly recognizable as a jQuery object versus a JavaScript object to anyone who knows. Very cool!!


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
  •