$函数在香草(无库)JS中意味着什么?

What does $ function means in vanilla (no libraries) JS?

我想知道 $ sign 函数是什么意思。

它只是document.querySelector的捷径还是有更多用途。

我转到 about:blank 页面并在控制台中输入 $.

Firefox 中的结果是:

function()

并在 Chrome 中:

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

它作为 document.querySelector() 的快捷方式,但我不确定它叫什么以及浏览器对此的支持是什么。

还有一个 $$ 函数作为 document.querySelectorAll(),

$_ 存储最后一个 $/$$ 函数结果,

[=15=] 仅参考 document.body(我认为)

我认为

$x 是 XPath 查询,因为 xpath 参数:

ƒ $x(xpath, [startNode]) { [Command Line API] }

(在控制台输入 $x 时输出 chrome)

正如“[命令行 API]”消息所暗示的那样,它是浏览器内置的便利功能,而不是原版 javascript 的一部分。

对于Chrome,例如:

The Console Utilities API contains a collection of convenience functions for performing common tasks: selecting and inspecting DOM elements, displaying data in readable format, stopping and starting the profiler, and monitoring DOM events. https://developers.google.com/web/tools/chrome-devtools/console/utilities

Safari 和 Firefox 似乎支持与 Chrome 相同的一组功能;我相信(但不确定)这是共识而非实际标准。

这些功能只能在开发者控制台中使用;它们在其他地方不可用。

console.log($) // will throw "ReferenceError: Can't find variable: $"

$字符本身在javascript中没有特殊意义,可以作为任何其他字符字形使用:

var $ = "hello"
var a$b = "world"
console.log($, a$b)

$ = function() {console.log("This will confuse jQuery users, probably not a great idea")}
$()

您发现了每个浏览器的开发者工具中内置的非标准化标识符。它们不是普通 JS 的一部分。

请注意,您不能在常规脚本中使用这些助手。 根据使用的库或编写这些标识符的上下文,与内置助手相比,它们可能具有完全不同的含义,因为它们可以在那里任意分配。