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


推荐阅读
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
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社区 版权所有