热门标签 | 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


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
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社区 版权所有