$ 与 jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2212602/
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
$ versus jQuery
提问by JamesBrownIsDead
We're switching from MooTools to jQuery at work. I have a coworker that told me never to use $
as the prefix when using a jQuery method (if I understood him correctly). He said that instead, the more appropriate or safer way (I didn't really follow him) was to use jQuery.
.
我们正在从 MooTools 切换到 jQuery。我有一个同事告诉我$
在使用 jQuery 方法时不要用作前缀(如果我理解正确的话)。他说,相反,更合适或更安全的方法(我并没有真正关注他)是使用jQuery.
.
So instead of $.plugin()
, jQuery.plugin()
should be used, for example.
因此,例如$.plugin()
,jQuery.plugin()
应该使用 , 代替。
Why would that be the case? What distinction is he making with $
/jQuery
? Should I forget about the dollar sign as my accessor? Only in certain circumstances?
为什么会是这样?他与$
/ 有什么区别jQuery
?我应该忘记美元符号作为我的访问者吗?仅在某些情况下?
回答by Rafael
Why would that be the case? Is his $/jQuery distinction correct?
为什么会是这样?他的 $/jQuery 区分是否正确?
Because almost every JavaScript library defines a function called $. When you have many libraries in one document, conflicts may appear. If you are sure that jQuery is and always will be the only script defining that function, I wouldn't have anything against using $.
因为几乎每个 JavaScript 库都定义了一个名为 $ 的函数。当您在一个文档中有多个库时,可能会出现冲突。如果您确定 jQuery 是并且永远是定义该函数的唯一脚本,那么我不会反对使用 $。
jQuery defines a nice solution to resolve conflicts: jQuery.noConflict. By using this function you can define your own name, whereby jQuery will be accessible, e.g.
jQuery 定义了一个很好的解决冲突的解决方案:jQuery.noConflict。通过使用此函数,您可以定义自己的名称,从而可以访问 jQuery,例如
var $jq = jQuery.noConflict(true);
Calling this function will result in restoring previous values for $ and jQuery variables when existed before initializing jQuery. I don't remember if any other libraries try to resolve name conflicts.
调用此函数将导致 $ 和 jQuery 变量在初始化 jQuery 之前存在时恢复以前的值。我不记得是否有任何其他库尝试解决名称冲突。
If you want to use $ instead of jQuery all the time you can run your code in a separate, private scope that holds the definition of $ by using a self-invoking function.
如果您想一直使用 $ 而不是 jQuery,您可以在单独的私有范围中运行您的代码,该范围通过使用自调用函数来保存 $ 的定义。
(function($){
// your code goes here
$("body").append("<p>Hello World!</p>");
})(jQuery); // or even jQuery.noConflict()
回答by Matt Lacey
$ is aliased to jquery and could, in theory, be aliased to something else in another included library or script which may lead to confusion or worse. If you're only using jquery, using $ should be fine.
$ 是 jquery 的别名,理论上,它可以是另一个包含的库或脚本中的其他东西的别名,这可能会导致混淆或更糟。如果您只使用 jquery,使用 $ 应该没问题。
回答by Dezza
In addition to the other answers around conflicts with other libraries, a variable name should always describe the variable. jQuery
is a much more descriptive name for a variable than $
.
除了与其他库冲突的其他答案外,变量名称应始终描述变量。jQuery
是一个比变量更具描述性的名称$
。
回答by Omar Abid
It's to avoid conflict with other libraries like Prototype that uses the same $.
这是为了避免与使用相同 $ 的其他库(如 Prototype)发生冲突。
I prefer the $, and there is no problem using it if you are using only Jquery.
我更喜欢 $,如果您只使用 Jquery,使用它没有问题。