jQuery $美元符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180213/
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-08-26 10:50:53  来源:igfitidea点击:

The $ dollar sign

jquery

提问by Mark

I have something simple like this:

我有这样简单的事情:

$(selector).append("somestuff");

But since I'm going to reuse the selector I cache it with:

但是由于我要重用选择器,所以我将它缓存为:

var $selector = $(selector);

So I end up with:

所以我最终得到:

$selector.append("somestuff");

My question is, should I be doing that, or should I be doing:

我的问题是,我应该这样做,还是应该这样做:

var selector = $(selector);
selector.append("somestuff");

Trying either one, both works. Which method is correct, and why? Is the $in $selectorunnecessary because the jquery object has already been declared in $(selector)?

尝试任何一种,都有效。哪种方法是正确的,为什么?是$$selector不必要的,因为jQuery对象已经在申报$(selector)



Edit

编辑

Thanks for the answers. It seems very simple and quite clear. Still, there seems to be disagreement over whether or not I should use $in the variable. It would be nice for everyone to vote up an answer. :)

感谢您的回答。这看起来很简单,也很清楚。尽管如此,对于我是否应该$在变量中使用似乎存在分歧。每个人都投票赞成答案会很好。:)

采纳答案by Michael Morton

I've seen it done both ways. All you are doing is creating a variable with the name '$selector', so they are functionally equivalent. The nice thing about it is that it does make them easy to pick out as jQuery objects.

我已经看到它以两种方式完成。您所做的只是创建一个名为“$selector”的变量,因此它们在功能上是等效的。它的好处是它确实使它们很容易被挑选为 jQuery 对象。

回答by RichieHindle

$is just a name - names in JavaScript can contain dollar signs, and can consist of just a dollar sign.

$只是一个名字 - JavaScript 中的名字可以包含美元符号,并且可以只包含一个美元符号。

Whether you use a dollar sign in your name isn't relevant to jQuery - there's nothing special about the dollar sign, except that jQuery defines a function called $.

是否在名称中使用美元符号与 jQuery 无关 - 美元符号没有什么特别之处,只是 jQuery 定义了一个名为$.

回答by Ben Crouse

The cash sign is just an alias for the jQuery function. Starting the variable name with $ has no effect on that variable.

现金符号只是 jQuery 函数的别名。以 $ 开头的变量名对该变量没有影响。

It is customary though, especially in jQuery plugin development, to use variables already wrapped in jQuery with a cash sign, so you know you can call jQuery methods, without having to wrap them.

不过,习惯上,尤其是在 jQuery 插件开发中,使用已经包装在 jQuery 中的带有现金符号的变量,因此您知道您可以调用 jQuery 方法,而无需包装它们。

回答by thedz

RichieHindle is correct. To expand:

RichieHindle 是正确的。扩张:

Javascript variables allow the '$' character. So for example, you could have the following:

Javascript 变量允许使用 '$' 字符。因此,例如,您可以具有以下内容:

var $i = 1;
var i = 1;

Both i and $i have the same value, and both are perfectly legitimate variable names.

i 和 $i 具有相同的值,并且都是完全合法的变量名。

jQuery assigns itself (the jQuery object) to '$' because traditionally, that's what Javascript frameworks have done for selectors. There's no inherent meaning to '$' beyond what jQuery gives it.

jQuery 将自身(jQuery 对象)分配给 '$',因为传统上,这是 Javascript 框架为选择器所做的。'$' 除了 jQuery 赋予的意义之外,没有任何内在的意义。

回答by mkoryak

I like to prefix all my jQuery object names with $so that I know it's actually a jQuery object, not a DOM reference.

我喜欢在我所有的 jQuery 对象名称前加上前缀,$这样我就知道它实际上是一个 jQuery 对象,而不是一个 DOM 引用。

It's a good naming convention.

这是一个很好的命名约定。

回答by Zachary K

In this case, $selectoris an example of Hungarian notation. It is there so that you know that $selectoris a jQuery object.

在这种情况下,$selector是匈牙利符号的一个例子。它在那里,以便您知道这$selector是一个 jQuery 对象。

If you make it your custom to always have jQuery objects start with $if you suddenly use one wrong it will look wrong which will help you find a problem in your code.

如果您习惯于始终以 jQuery 对象开头,$如果您突然使用了一个错误,它看起来会出错,这将帮助您找到代码中的问题。

The language couldn't care less. They could have just as well named the jQuery function Jor anything else; the $is not a special symbol.

语言根本不在乎。他们也可以命名 jQuery 函数J或其他任何东西;在$不是特殊符号。

回答by easement

Yeah.

是的。

jQuery always returns a jQuery object. It's what allows chaining.

jQuery 总是返回一个 jQuery 对象。这就是允许链接的原因。

You've already defined the variable so you get a jQuery object back so $ is unnecessary

你已经定义了变量,所以你得到了一个 jQuery 对象,所以 $ 是不必要的

回答by Grant Heaslip

I don't believe that you need to add the jQuery selector to jQuery objects (since it's already a part of the object). We don't add the selector and haven't run into any problems.

我不认为您需要将 jQuery 选择器添加到 jQuery 对象(因为它已经是对象的一部分)。我们没有添加选择器,也没有遇到任何问题。

回答by vh.

"$" is a function in jQuery. So when you call $(selector), you're actually calling the function $with selectoras the argument.

"$" 是 jQuery 中的一个函数。所以,当你调用$(选择),你实际上调用该函数$selector作为参数。

Generally, don't use the "$" as part of a variable name for javascript. You will only confuse yourself.

通常,不要使用“$”作为 javascript 变量名的一部分。你只会迷惑自己。