简体   繁体   English

Javascript 中的美元符号是什么,如果不是 jQuery

[英]What is the dollar sign in Javascript, if not jQuery

I did some javascript / jQuery programming a few years ago, and I just started up again.几年前我做了一些 javascript / jQuery 编程,然后我又重新开始了。 Back then the dollar sign was used for all the jQuery functionality, and if no jQuery library was imported, the dollar sign was not defined.当时美元符号用于所有 jQuery 功能,如果未导入 jQuery 库,则未定义美元符号。

Today, I started up Firefox, in a completely empty html file with no javascript libraries, and yet the dollar sign points to something.今天,我在一个没有 javascript 库的完全空的 html 文件中启动了 Firefox,但美元符号指向了某些东西。 If I open the Firefox console and type '$' I get "function()" .如果我打开 Firefox 控制台并输入'$'我得到"function()"

1) Is it correct that dollar sign was not assigned, a few years back, or do I remember wrong? 1)几年前没有分配美元符号是正确的,还是我记错了?

2) What is the dollar sign , if not jQuery? 2) 如果不是 jQuery,美元符号是什么?

1) Is it correct that dollar sign was not assigned, a few years back, or do I remember wrong? 1)几年前没有分配美元符号是正确的,还是我记错了?

That's correct and still true.这是正确的,而且仍然正确。

2) What is the dollar sign, if not jQuery? 2) 如果不是 jQuery,美元符号是什么?

Firefox and Chrome implement $ , $$ and several others as helper commands. FirefoxChrome实现了$$$和其他几个作为辅助命令的命令。 Both will set $$ to document.querySelectorAll() , and set $ to document.querySelector if window.$ hasn't been defined.两者都将$$设置为document.querySelectorAll() ,如果window.$尚未定义,则将$设置$ document.querySelector

So what you're seeing isn't actually standard JavaScript, but a helper in the developer console of your browser.因此,您所看到的实际上并不是标准的 JavaScript,而是浏览器开发者控制台中的一个助手。 It's also not jQuery (as long as you're not on a page using jQuery).它也不是 jQuery(只要您不在使用 jQuery 的页面上)。 However, it's behaviour is close to the one of jQuery , concerning that querySelector (for single matches) and querySelectorAll (for multiple matches) give you almost the same strength as the jQuery selector.然而,它的行为接近于jQuery ,关于querySelector (对于单个匹配)和querySelectorAll (对于多个匹配)给你几乎与 jQuery 选择器相同的强度。

It means nothing to the interpreter just like the underscore它对解释器没有任何意义,就像下划线一样

From the ECMAScript specification:来自 ECMAScript 规范:

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier.美元符号 ($) 和下划线 (_) 可用于标识符中的任何位置。 The dollar sign is intended for use only in mechanically generated code.美元符号仅用于机械生成的代码。

You may also check JavaScript Dollar Sign ($) - What is it for?您还可以查看JavaScript 美元符号 ($) - 它是做什么用的?

By convention, the dollar sign ($), the underscore (_) and even some ASCII character are permitted to be used anywhere in a JavaScript identifier (Source: Ecma Script documentation (7.6 Identifiers, ECMA-262, 3rd Ed.) the dollar sign is intended for use only in mechanically generated code. This means that we do not want to use the dollar sign ($) in our indentifier names, unless we are writing a framework. The following is a list of permitted characters that can be used anywhere in an identifier name:按照惯例,美元符号($),下划线(_),甚至一些ASCII字符被允许可以在JavaScript标识符(来源任何地方使用:ECMA脚本文件(7.6标识符,ECMA-262,第三版)美元sign 仅用于机械生成的代码。这意味着我们不想在标识符名称中使用美元符号 ($),除非我们正在编写框架。以下是可以使用的允许字符列表标识符名称中的任何位置:

IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
UnicodeEscapeSequence
IdentifierPart ::
IdentifierStart
UnicodeCombiningMark
UnicodeDigit
UnicodeConnectorPunctuation
UnicodeEscapeSequence

EDIT:-编辑:-

Actually dollar sign function has become the more-or-less de facto shortcut to document.getElementById() .实际上,美元符号函数已成为document.getElementById()或多或少事实上的快捷方式。

To confirm my point check this :为了证实我的观点检查

$(selector) $(选择器)

Returns a single element matching the given CSS selector.返回与给定 CSS 选择器匹配的单个元素。 In old Firebug versions , this used to be equivalent to document.getElementById .在旧的 Firebug 版本中,这曾经等同于document.getElementById

Dollar sign($) was not assigned, but some browser add function for special usage. Dollar sign($)未分配,但某些浏览器添加了特殊用途的功能。

Like Google Chrome, if you type $ on the console, it will return :像谷歌浏览器一样,如果你在控制台输入$ ,它会返回:

function $(selector, [startNode]) { [Command Line API] }

This function assigned for Google Chrome Developer Tool, and let debug more easier.这个功能分配给谷歌浏览器开发者工具,让调试更容易。

if you type $('div') , it will return something like this:如果您输入$('div') ,它将返回如下内容:

e.fn.e.init[178]

and include every div DOM object in it.并在其中包含每个div DOM 对象。

BTW, after you click the right button on the mouse to select element, you can acceess angular.js scope by type $scope on the console顺便说一句,单击鼠标右键选择元素后,您可以通过在控制台上键入$scope来访问 angular.js 范围

Note that $$ isn't quite document.querySelectorAll , because unlike that function it doesn't return a NodeList :请注意, $$并不完全是document.querySelectorAll ,因为与该函数不同,它不返回NodeList

document.querySelectorAll("p") instanceof NodeList // true

$$("p") instanceof NodeList // false

Array.isArray($$("p")) // true

So $$(selector) is really more like Array.from(document.querySelectorAll(selector)) .所以$$(selector)真的更像Array.from(document.querySelectorAll(selector)) This means that array methods like map and friends, not just forEach , are available when using $$ which is actually quite useful.这意味着在使用$$时可以使用像map和朋友这样的数组方法,而不仅仅是forEach ,这实际上非常有用。

It may be anything, as $ is a valid variable name, just like dollar .它可以是任何东西,因为$是一个合法的变量名,就像dollar

From ECMAScript :ECMAScript

Identifier ::
IdentifierName but not ReservedWord

IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart

IdentifierStart ::
UnicodeLetter
$
_ 
\ UnicodeEscapeSequence

The simplest solution to see what it is and where it is defined would probably to type $() and to put a breakpoint on this line.查看它是什么以及它在哪里定义的最简单的解决方案可能是键入$()并在此行上放置一个断点

Just to complete the other answers here, MooTools also uses $ as a alias to document.getElementById .只是为了完成这里的其他答案,MooTools 还使用$作为document.getElementById的别名。

It does check if the $ is taken and will then default to document.id .它会检查$是否被占用,然后默认为document.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM