javascript "jQuery(...)" 而不是 "$(...)"

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

"jQuery(...)" instead of "$(...)"

javascriptjquery

提问by Mahdi

Is it a good practice to write jQuery("...")instead of $("...")?

jQuery("...")而不是写是一个好习惯$("...")吗?

I heard that it might be useful for avoiding future conflicts with other libraries if they use the $as well, but is that a real-world concern? Let say I really have no idea what other libraries I'm gonna use in the future, so should I avoid writing $("...")in my scripts? Is there any other well known libraries relying on $as well?

我听说如果其他库也使用 ,它可能有助于避免将来与其他库发生冲突$,但这是现实世界的问题吗?假设我真的不知道我将来会使用哪些其他库,那么我应该避免$("...")在我的脚本中编写吗?还有其他著名的图书馆也依赖$吗?

Also I'd love to know if there is any other issues attached to this subject.

我也很想知道这个主题是否还有其他问题。

采纳答案by Dory Zidon

It's perfectly safe to use the $ for jQuery, it's the jQuery idiom, at least on your own pages which you control.

将 $ 用于 jQuery 是完全安全的,它是 jQuery 习惯用法,至少在您自己控制的页面上是这样。

If you do choose to use jQuery in widgets you can structure it in a way that prevents it from conflicts.

如果您确实选择在小部件中使用 jQuery,您可以以一种防止它发生冲突的方式来构建它。

In general jQuery binds to both $ and jQuery so using jQuery alone does not guarantee you safey..

通常,jQuery 绑定到 $ 和 jQuery,因此单独使用 jQuery 并不能保证您的安全。

If you are concerned with conflict jQuery does expose the $.noConflit(); http://api.jquery.com/jQuery.noConflict/

如果您担心冲突,jQuery 会公开 $.noConflit(); http://api.jquery.com/jQuery.noConflict/

Which relinquishes the control of $ and you can store jQuery in a variable of your choice (or use jQuery).

这放弃了 $ 的控制权,您可以将 jQuery 存储在您选择的变量中(或使用 jQuery)。

Here are the libraries that use the $ sign based on the Wikipedia pageand this stackoverflow answer:

以下是基于Wikipedia 页面和此stackoverflow 答案使用 $ 符号的库:

Scanning this page, the only ones that come to mind are:

  1. jQuery - Provides jQuery.noConflict() to release the $
  2. Prototype
  3. MooTools

扫描这个页面,唯一想到的是:

  1. jQuery - 提供 jQuery.noConflict() 来释放 $
  2. 原型
  3. MooTools

However jQuery is killing it with about 90% market share..

然而,jQuery 以大约 90% 的市场份额杀死了它。

  1. http://www.theregister.co.uk/2012/08/14/jquery_used_by_half_of_all_websites/
  2. https://stackoverflow.com/questions/176324/why-does-everyone-like-jquery-more-than-prototype-script-aculo-us-or-mootools-or
  1. http://www.theregister.co.uk/2012/08/14/jquery_used_by_half_of_all_websites/
  2. https://stackoverflow.com/questions/176324/why-does-everyone-like-jquery-more-than-prototype-script-aculo-us-or-mootools-or

回答by Vitalii Petrychuk

You can wrap your code with sell-invoking function and use name you want

您可以使用销售调用功能包装您的代码并使用您想要的名称

(function (JQUERY, otherLIB) {
  // ...
}(jQuery, $))

回答by Vitalii Petrychuk

If you're using the .ready()function (or shortcut version) to wrap your code, then there's already functionality built in to give you safe access to the jQuery library. That is through defining a function parameter in the callback you provide.

如果您使用该.ready()函数(或快捷版本)来包装您的代码,那么已经有内置功能可以让您安全访问 jQuery 库。那是通过在您提供的回调中定义一个函数参数。

jQuery(document).ready(function($) { 
    /* in here, $ is safe */ 
}); 

jQuery(function($) { 
    /* in here, $ is safe */ 
});

Since jQuery passes itself as the first argument to the callback, you can just give it any name that you want, including $. Because it forms a local parameter, it will be unaffected by and will not affect an identical name in the global space.

由于 jQuery 将自身作为第一个参数传递给回调,因此您可以给它任何您想要的名称,包括$. 因为它形成了一个局部参数,它不会受到全局空间中相同名称的影响,也不会影响它。

This way there's no need for .noConflict()or manual IIFE approaches. Everything you need is already right there for you.

这样就不需要.noConflict()或手动 IIFE 方法。您需要的一切都已经为您准备好了。

回答by David Barker

The $is just a shorthand namespace, due to jQuery being very well known many javascript frameworks tend to view $as a reserved namespace and shy away from using it. That said it is very easy for anyone to namespace any code to window.$.

$只是一个简写的命名空间,由于 jQuery 是众所周知的,许多 javascript 框架倾向于将其$视为保留的命名空间并回避使用它。也就是说,任何人都可以轻松地将任何代码命名为window.$.

If you are conscious about what the $namespace refers to exactly you can remove jQuerys bindings to that namespace and rebind it yourself outside of the global scope.

如果您知道$命名空间究竟指的是什么,您可以删除与该命名空间的 jQuery 绑定,并在全局范围之外重新绑定它。

jQuery.noConflict();

(function($) {

    // your code that can use $ as jQuery namespace

})(jQuery);

回答by Adil

$ is now used frequently and might be use by other libraries event if you do not use it. You can use jQuery.noConflict.

$ 现在经常使用,如果您不使用它,可能会被其他库事件使用。您可以使用jQuery.noConflict

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict(). Old references of $ are saved during jQuery initialization; noConflict() simply restores them, Reference.

许多 JavaScript 库使用 $ 作为函数或变量名,就像 jQuery 一样。在 jQuery 的情况下,$ 只是 jQuery 的别名,因此所有功能都可以使用而无需使用 $。如果您需要与 jQuery 一起使用另一个 JavaScript 库,请通过调用 $.noConflict() 将 $ 的控制权返回给另一个库。$ 的旧引用在 jQuery 初始化期间被保存;noConflict() 只是恢复它们,Reference。

回答by apparat

Some other libraries use the dollar sign as well. Mootools for examplecan use it too. But there is always the possibility to use jQuerys noConflictmode, so you can quite safely use the dollar sign in your code.

其他一些图书馆也使用美元符号。例如 Mootools也可以使用它。但是总是有可能使用 jQuery 的noConflict模式,因此您可以非常安全地在代码中使用美元符号。