简体   繁体   English

我需要在javascript变量名之前加一美元吗?

[英]Do I need to put a dollar before variable names with javascript?

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"? 我需要“var $ form = form”这一行吗? Can I just later on do "var val = form.valid(); ? 我可以稍后再做“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) ? 如果已经说过它只是表明它是一个jQuery变量那么我可以通过将函数参数更改为(link,$ form)来删除该行吗?

$ and jQuery are basically the jQuery instance. $jQuery基本上就是jQuery实例。

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. 很高兴理解$( < place something here >)是一个jQuery函数调用, $your_variable_name只是一个带有美元的变量。

Some people use $ in their own variables to indicate that it is a jQuery object. 有些人在自己的变量中使用$来表示它是一个jQuery对象。 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) . 在上面的示例代码中,函数的参数名为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 . 在函数内部,声明的新变量有一个$ ,以便将它与参数变量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变量。 JQuery uses the dollar sign as a shortcut. JQuery使用美元符号作为快捷方式。 Using it in a variable name has no extra meaning other than the aesthetic meaning for the developer that it is a JQuery object. 在变量名中使用它没有额外的意义,除了开发人员的美学意义,它是一个JQuery对象。

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. 最佳实践是使用$符号来帮助您区分表示常规DOM元素(和其他所有数据类型)的Javascript变量以及包含对jQuery对象的引用的变量。 For the latter you use the $ sign. 对于后者,您使用$符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在JavaScript中的某些构造函数之前看到一个美元符号? - Why do I see a dollar sign before some constructors in JavaScript? 我如何返回这个 function 并在它前面加上一个美元符号。 (Javascript) - How do I return this function with a dollar sign before it. (Javascript) Javascript:我是否需要为对象中的每个变量都放置 this.var? - Javascript: Do I need to put this.var for every variable in an object? 我需要在异步函数中的每个语句之前放置 await 吗? - Do I need to put await before every statement in an async function? 如何在Javascript中添加美元金额 - How do I add to dollar amounts in Javascript 我需要一种变通方法来将带有破折号的Liquid变量分配给javascript变量名称 - I need a workaround for assigning Liquid variables with dashes to javascript variable names 如果我想用“ $$”(两美元)作为Java- / coffescript-中的替换字符串,为什么我需要“ $$$”(三美元) - Why do I need “$$$” (three dollar) if I want “$$” (two dollar) as replace string in Java- / coffescript- 如何在JavaScript数组中放入JavaScript变量? - How do I put a javascript variable inside javascript array? 为什么在函数名称之前需要写“ javascript:”? - Why do I need to write “javascript:” before a function name? javascript美元符号变量不工作 - javascript dollar sign variable not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM