Sunday, September 6, 2009

Today, I got little bit of time and want to share with you small things I learn in JQuery. I know, most of the people who are new to Jquery don't know what is $ in JQuery.

$ is the symbol which is the initialization of Jquery object. You can pass selector, function, string etc to execute or implement some functionality. So, $("#idOfElement") or JQuery("#idOfElement") both are same.

I saw many people are declaring the Jquery variables as shown below.
var currentObject = null;
currentObject = $("#someID");

So, I don't say this is the best practice to use it. Instead you can do the below.
var $currentObject = null;
$currentObject = $("#someID");
And if you want to use it, you can directly use it as shown below.
$currentObject.val() or any other function supported for the current object.

So, the advantage here is, for easily identifying the Jquery variables. If you are working on a project where it needs lot of Jquery coding, then you must use this practice. Hope this helps!
Categories:

10 comments:

  1. Nice to know Jquery variables. Thanks for the explanation.

    ReplyDelete
  2. Nice to know, i think this will be something god from performance point of view.

    ReplyDelete
  3. I am not sure what your trying to say!

    var currentObject;
    currentObject = $("#someID");

    is the same as
    var $currentObject;
    $currentObject = $("#someID");

    The $ in front of the variable does not give it any special properties. matter of fact I can do this on both of the above versions.

    currentObject.val()
    and
    $currentObject.val()

    If you want to put the dollar sign in front of your variable to help you recognize a JQuery dom element over a regular for code readability then go ahead. The $ is just a character in a variable name.

    ReplyDelete
  4. If I may clarify what you are stating. Prefixing a dollar sign to a variable name does not inherently make the variable a jQuery object. EMCAScript and Javascript allow the dollar sign in variable names.

    var $thisVarIsANumber = 17;
    var this$var$is$another$number = 31;

    You can name jQuery variables with or without dollar signs:

    var thisVarIsAJQueryObject = $("#someID");
    var $thisVarIsAnotherJQueryObject = $("#someID");

    The following three lines will hide the same DOM element:

    $("#someID").hide();
    thisVarIsAJQueryObject.hide();
    $thisVarIsAnotherJQueryObject.hide();

    It can serve as a helpful hint if you prefix only your jQuery variables with a dollar sign but it is up to you and your fellow programmers to follow this convention, like Hungarian notation.

    http://en.wikipedia.org/wiki/Hungarian_notation

    ReplyDelete
  5. Thanks a lot for your valuable comments. And this is what i tested already and don't know why that didn't work for me earlier. Now, when I use same code, it is working without $ symbol before variable. Are there any problems with Jquery versions? I am using Jquery 1.3.2 now.

    -Praveen.

    ReplyDelete
  6. Thanks..your stuff really helped

    ReplyDelete
  7. There are all goofy stuffs. No difference between $ and without $.
    Think before posting something. It cuases our time.

    ReplyDelete
  8. Hi Tania,
    This post is just to understand for easily identifying the jquery variables if you have huge amount of javascript written. If a variable has $ in it then we can easily identify it as jquery variable.

    thanks
    -Praveen.

    ReplyDelete
  9. @Praveen, +1 for putting it online, though you could have used a better formatting and language.

    @Tania, it doesn't matter in a small world where you know your app in and out. But when you have an enterprise application where JS modules interact with each other, the so called "goofy stuff" helps a much to speed up the development. "Writing clean code" practices emphasize on these. Given a bunch of 5 sticks and a calculator I can do 2 plus 3 in different ways, but which one is better?

    Thanks!

    ReplyDelete