最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Do I need to put a dollar before variable names with javascript? - Stack Overflow

programmeradmin6浏览0评论

I have the following code:

var formSubmitHandler = function (link, form) {

    //e.preventDefault();
    var $form = form;
    var val = $form.valid();
    var action = $(form).data('action');
    var entity = $(form).data('entity');

Do I need the line "var $form = form"? Can I just later on do "var val = form.valid(); ?

It's not my code so I am wondering why the developer added the $ before the form and assigned it?

Update:

Thanks for all your replies. If as has been said it's just to indicate that it's a jQuery variable then could I just remove that line by changing the function parameters to (link, $form) ?

I have the following code:

var formSubmitHandler = function (link, form) {

    //e.preventDefault();
    var $form = form;
    var val = $form.valid();
    var action = $(form).data('action');
    var entity = $(form).data('entity');

Do I need the line "var $form = form"? Can I just later on do "var val = form.valid(); ?

It's not my code so I am wondering why the developer added the $ before the form and assigned it?

Update:

Thanks for all your replies. If as has been said it's just to indicate that it's a jQuery variable then could I just remove that line by changing the function parameters to (link, $form) ?

Share Improve this question edited May 4, 2012 at 6:09 asked May 4, 2012 at 6:02 user1321237user1321237 2
  • 2 possible duplicate of Why would a JavaScript variable start with a dollar sign? – No Results Found Commented May 4, 2012 at 6:05
  • Sometimes, the $ before a variable name is to indicate that it is a jQuery object and not, say, a raw DOM element. That is, you might say var $this = $(this) with the $ in front to indicate that it's a jQuery object. On the other hand, you might have var element = document.getElementById('test'). It's all up to the developer, of course. – Reid Commented May 4, 2012 at 6:05
Add a comment  | 

4 Answers 4

Reset to default 12

$ and jQuery are basically the jQuery instance.

It's good to understand that $( < place something here >) is a jQuery function call and $your_variable_name is just a variable with a dollar.

Some people use $ in their own variables to indicate that it is a jQuery object. With this naming convention, your source code would like this.

var formSubmitHandler = function (link, form) {
    var $form = $(form);
    var val = $form.valid();
    var action = $form.data('action');
    var entity = $form.data('entity');

No, you don't have to use the $ sign. It's just an aesthetic choice usually.

In your example code above, the function's argument is named form function (link, form). Inside the function, the new variable being declared has a $ to it so as to distinguish it from the argument variable form.

That is not neccessary. The dollar sign before a variable is most of the times used as an indication it is a JQuery variable. JQuery uses the dollar sign as a shortcut. Using it in a variable name has no extra meaning other than the aesthetic meaning for the developer that it is a JQuery object.

It has become best practice to use the $ sign to help you distinguish between Javascript variables representing regular DOM elements (and every other data type) and variables that hold a reference to a jQuery object. For the latter you use the $ sign.

发布评论

评论列表(0)

  1. 暂无评论