热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在jQuery糟糕的做法中使用$()快捷方式?-isusingthe$()shortcutinjQuerybadpractice?

Iwasrecentlylisteningtoapodcastwhichmadeacommentonusing$()vsusingjQuery().Itwasst

I was recently listening to a podcast which made a comment on using $() vs using jQuery(). It was stated that every time $() was used a new object would be created and when jQuery() was used this was not the case. I google'd around but couldn't find anything on this specific topic.

我最近在收听一个播客,该播客对使用$()vs jQuery()进行了评论。据说,每次使用$()时都会创建一个新对象,当使用jQuery()时,情况并非如此。我谷歌了,但在这个特定主题上找不到任何东西。

I realize this is not a typical example, but the following is the reason I am interested in the answer to this question.

我意识到这不是一个典型的例子,但以下是我对这个问题的答案感兴趣的原因。

I have a page that the user will keep loaded in a browser for a whole day (24 hours, or possibly longer) and updates are done to the DOM every ~5 seconds as the result of an AJAX call via jQuery (the AJAX call portion is irrelevant to updating the DOM - the update to the DOM is done using a string of HTML and a call on a jQuery object to .empty() and then .html()).

我有一个页面,用户将在浏览器中加载一整天(24小时,或可能更长),并且每隔约5秒对DOM进行一次更新,这是通过jQuery调用AJAX(AJAX调用部分)的结果与更新DOM无关 - 对DOM的更新是使用HTML字符串和jQuery对象调用.empty()然后调用.html())完成的。

Since hearing this, I subsequently switched all of the $() calls to jQuery() calls, but I would like to know:
Is using $() vs using jQuery() a bad practice? Is there a negligible difference between the two? Or is it actually noticeable on larger projects?

听到这个,我随后将所有$()调用切换到jQuery()调用,但我想知道:使用$()vs使用jQuery()是一种不好的做法吗?这两者之间的差异是微不足道的吗?或者它在大型项目中是否真的值得注意?

6 个解决方案

#1


21  

No, it's not bad practice, and there is no performance difference.

不,这不是不好的做法,并没有性能差异。

The $ and jQuery identifiers refer to the same function instance.
This is done by the last line of jQuery.js:

$和jQuery标识符引用相同的函数实例。这是由jQuery.js的最后一行完成的:

window.jQuery = window.$ = jQuery;

#2


12  

The only problem with using $() over jQuery() is the possibility that another Javascript framework uses it as well.

使用$()而不是jQuery()的唯一问题是另一个Javascript框架也可能使用它。

#3


4  

Nope - take a look at the jQuery source code. $ is just another alias for jQuery - the last line says it all:

不 - 看看jQuery源代码。 $只是jQuery的另一个别名 - 最后一行说明了一切:

window.jQuery = window.$ = jQuery;

See here for yourself: http://code.jquery.com/jquery-latest.js

请看这里:http://code.jquery.com/jquery-latest.js

#4


2  

To me, the goal is to avoid naming collision with other libraries that also use $ as main object, like Prototype, if you want to use both libraries on the same page, or you don't know where your code will be used...

对我来说,目标是避免命名与其他使用$作为主要对象的库的命名冲突,如Prototype,如果你想在同一页面上使用这两个库,或者你不知道你的代码将被用在哪里。 。

#5


2  

Are you sure it was $() vs jQuery()? Maybe the more salient point is that there are performance hits to doing either, and many new js coders use $() unnecessarily when plain js could do.

你确定它是$()vs jQuery()吗?也许更突出的一点就是有性能命中要么做,而且许多新的js编码器在普通的js可以做的时候不必要地使用$()。

It's good practice to avoid creating a jQuery object when you don't have to.

最好避免在不必要时创建jQuery对象。

#6


0  

As stated before, the only real problem is getting into conflict with other js frameworks used, therefore i find this is the most handy solution to be still able to use the dollar sign, but have no conflicts and make your code more portable:

如前所述,唯一真正的问题是与使用的其他js框架发生冲突,因此我发现这是仍然能够使用美元符号的最方便的解决方案,但没有冲突并使您的代码更具可移植性:

(function($) { /* some code that uses $ */ })(jQuery)

http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Referencing_Magic_-_Shortcuts_for_jQuery

http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Referencing_Magic_-_Shortcuts_for_jQuery


推荐阅读
author-avatar
摄影爱好者Summer_100
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有