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


推荐阅读
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Javascript中带有加号 - 减号(±)的极坐标曲线方程 - Polar curve equation with plus-minus sign (±) in Javascript
    IamtryingtodrawpolarcurvesonHTMLcanvasusingJavascript.WhatshouldIdowhenIwanttoco ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和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社区 版权所有