为什么 jQuery 内部需要 Sizzle?

Why jQuery needs Sizzle internally?

当我查看 jQuery 代码源时,我对 Sizzle-js 产生了兴趣,这是一个 CSS 选择器引擎库,jQuery 使用它来选择 DOM 元素。所以,我的问题是,为什么 jQuery 包含一个仅用于选择 DOM 元素的整个库?引入 Sizzle 的哪些功能让我们 jQuery 在内部使用它?

为了回答这个问题,我对Sizzle and his features, the main function of Sizzle is Sizzle(selector), this function returns an array-like object that contains some methods that allow you to operate on the DOM elements you've isolated, and this is very helpful actually if your application touched the DOM frequently. But, in my opinion, this isn't worth to include a 20KB of code, I know that Sizzle covered also the old browser versions that don't support the Document.querySelectorAll函数做了一些研究,但我仍然不服气!

实际上,你可以在几行代码中完成 Sizzle 所做的(模拟概念),我已经根据 querySelectorAll:

编写了一个非常简单的示例
var Select = function(selector) {
    if (typeof selector !== 'string') throw 'selector must be of type string';

    var client = function() {
        var eles = document.querySelectorAll(selector), i = eles.length;

        this.length = eles.length;

        while(i--) {
            this[i] = eles[i];
        }
    };

    client.prototype = Array.prototype;

    return new client();
};

console.log(Sizzle('div').includes(document.querySelector('div'))); // true
console.log(Select('div').includes(document.querySelector('div'))); // true

var divs = Select('div'); 

divs.forEach(function(div) {
    div.textContent = 'Changed!!';     
});

那么为什么 jQuery 仍然使用 Sizzle? document.querySelectorAlldocument.querySelector 现在可以在现代浏览器中使用,为什么 jQuery 仍然依赖于 Sizzle?

注意:有一个 old question 与此类似,但是,我对答案不满意,因为它更侧重于 什么是 Sizzle JS,以及什么可以做 ?,而不是 为什么 jQuery 使用 Sizzle?.

我希望我的问题很清楚,抱歉我的英语不好,谢谢。

jQuery 的选择器语法包括许多对 CSS 选择器的扩展,例如 :input:selected。您可以看到列表 here。这些扩展在 Sizzle 中实现。

如果可以,

jQuery 将使用 querySelectorAll(),但如有必要,将恢复为 Sizzle。