javascript 声明 jQuery 变量的官方正确方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4271028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:50:20  来源:igfitidea点击:

What is the official correct way to declare jQuery variables?

javascriptjquery

提问by chromedude

I am trying to learn jQuery. I keep seeing people using different methods of declaring variables.

我正在尝试学习 jQuery。我一直看到人们使用不同的方法来声明变量。

I have seen var variable;, var $variable;, and $variable;. Which one (if any of these) is the official correct way to declare jQuery variables.

我所看到的var variable;var $variable;$variable;。哪一个(如果有的话)是声明 jQuery 变量的官方正确方法。

回答by Nick Craver

You should always declare a variable with var(whatever the scope, unless creating it on an object), impliedglobals are bad, don't do it.

您应该始终声明一个变量var(无论范围如何,除非在对象上创建它),隐含的全局变量是不好的,不要这样做。

As for variablevs. $variable, there is no official stance, just have a convention that you and your team stick to.

至于variablevs. $variable,没有官方立场,只是有一个你和你的团队坚持的约定。

As a general rule somefollow, (I don't follow this, some do), use variablefor variables that aren't jQuery objects and $variablefor variables that are, those who use this typically find that seeing $somethingand knowing immediately it's a jQuery object to be handy.

作为一般规则,有些人遵循(我不遵循这个,有些人遵循这个规则),variable用于不是 jQuery 对象$variable的变量和那些使用它的变量,那些使用它的人通常会发现$something立即看到并知道它是一个 jQuery 对象得心应手。

回答by tvanfosson

Use varto define variables to ensure that they are scoped properly, usually locally within a function. My convention is to use $variablewhen the variable holds a jQuery object (result of a selector) and variablefor everything else.

使用var定义的变量,以确保它们正常范围的,通常在本地范围内的功能。我的约定是$variable在变量包含 jQuery 对象(选择器的结果)和variable其他所有内容时使用。

BTW -- they aren't jqueryvariables, but rather javascriptvariables. JQuery is just another (albeit the most popular at present) javascript framework, not a language in and of itself.

顺便说一句——它们不是jquery变量,而是javascript变量。JQuery 只是另一种(虽然是目前最流行的)javascript 框架,它本身并不是一种语言。

回答by Gabriele Petrioli

There is no official correct way to declare variables for jQuery..

没有为 jQuery 声明变量的官方正确方法..

They are all javascript variables, and these are the rules for javascript variables

它们都是javascript变量,这些是javascript变量的规则

Using the $at the beginning is a convention that the variable is holding a jQuery object (since the jquery uses the $ sign for the function wrapper)

$在开头使用的约定是变量保存一个 jQuery 对象(因为 jquery 使用 $ 符号作为函数包装器

The diffeernce of using varor not, is in the scope of the variable. Using vardeclares it local, not using varadds it to the global scope.

使用var与否的区别在于变量的范围内。Usingvar声明它是本地的,而不是 usingvar将它添加到全局范围。

回答by Felix Kling

Nick already explained why you should use var.

Nick 已经解释了为什么你应该使用var.

The reason behind variableand $variableis that $is associated with jQuery and should imply that this variable holds a jQuery object. To avoid unnecessary function calls to jQuery like $(variable)where variableactually already holds a jQuery object.

背后的原因variableand$variable$与 jQuery 相关联的,并且应该暗示这个变量包含一个 jQuery 对象。为了避免不必要的一样功能调用jQuery的$(variable)地方variable其实已经拥有jQuery对象。

回答by Guffa

There is no such thing as a "jQuery variable", they are Javascript variables. When learning jQuery you have to learn some Javascript, as jQuery is just a library for Javascript.

没有“jQuery 变量”这样的东西,它们是 Javascript 变量。在学习 jQuery 时,您必须学习一些 Javascript,因为 jQuery 只是一个 Javascript 库。

You declare a variable using the varkeyword. You can also use a variable without declaring it, and it will implicitly be declared as a global variable, but that is not recommended. You should declare variables in the scope where they are used.

您使用var关键字声明一个变量。您也可以使用变量而不声明它,它将隐式声明为全局变量,但不推荐这样做。您应该在使用它们的范围内声明变量。

Sometimes putting a $ sign as the first character of a variable name is used to indicate that it's intended for containing a reference to a jQuery object, however there is no official recommendation, and the language itself doesn't care what you name your variables.

有时将 $ 符号作为变量名的第一个字符用于表示它旨在包含对 jQuery 对象的引用,但是没有官方建议,并且语言本身并不关心您命名变量的名称。

回答by Marek Sapota

The ones with varare local variables, the one without it is global. So it's not just a matter of style. Apart from that use anything you like, personally I haven't seen "$" prefixed variables, but maybe they are useful if you mix a lot of jQuery and standard JavaScript code.

有的是var局部变量,没有的是全局变量。所以这不仅仅是风格问题。除了使用你喜欢的任何东西,我个人还没有看到“$”前缀变量,但如果你混合了很多 jQuery 和标准 JavaScript 代码,它们可能很有用。