阅读jquery源码问题记录(一)

阅读中该源码时不知道groups赋值的原理,经过度娘后有所了解,特此记录。

var a;
var b=[];
b.push(a=[]);
a=['1'];
console.log(b);//b=[[]];
var a;
var b=[];
b.push(a=[]);
a.push('1');
console.log(b);//b=[['1']];

在对a=[‘1’]时,改变的是a引用本身,没有改变数组对象,a和b[0]没有了关系。而在a.push(‘1’)时,改变的是数组对象,a引用没有改变。类似的还有pop();
更多具体相关内容可参考js的引用赋值和传值赋值.

function tokenize(selector, parseOnly) {
            var matched, match, tokens, type,
                soFar, groups, preFilters,
                cached = tokenCache[selector + " "];

            if (cached) {
                return parseOnly ? 0 : cached.slice(0);
            }

            soFar = selector;
            groups = [];
            preFilters = Expr.preFilter;

            while (soFar) {

                // Comma and first run
                if (!matched || (match = rcomma.exec(soFar))) {//如果soFar[0]==",'则进行匹配,match=[',',','];
                    if (match) {
                        // Don't consume trailing commas as valid
                        soFar = soFar.slice(match[0].length) || soFar;
                    }
                    groups.push(tokens = []);//没错就是它!!!!
                }

                matched = false;

                // Combinators
                if ((match = rcombinators.exec(soFar))) {//如果soFar[0]=="+ > ~ 空格'则进行匹配,match例如['+','+']
                    matched = match.shift();
                    tokens.push({
                        value: matched,
                        // Cast descendant combinators to space
                        // Cast descendant combinators to space
                        /*
                         * rtrim = new RegExp("^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)"
                         *          + whitespace + "+$", "g"),
                         * whitespace = "[\\x20\\t\\r\\n\\f]";
                         *
                         * 下面match[0].replace(rtrim, " ")的作用是将match[0]左右两边的空白替换为空格
                         * 但是由于其上的match.shift的作用,match[0]已经是两边不带空白的字符串了,
                         * 故此出的替换是没有用途的代码
                         */
                        type: match[0].replace(rtrim, " ")//rtrim = new RegExp("^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g"),
                    });
                    soFar = soFar.slice(matched.length);
                }

                // Filters
                for (type in Expr.filter) {
                    if ((match = matchExpr[type].exec(soFar)) && (!preFilters[type] ||
                        (match = preFilters[type](match)))) {
                        matched = match.shift();
                        tokens.push({
                            value: matched,
                            type: type,
                            matches: match
                        });
                        soFar = soFar.slice(matched.length);
                    }
                }

                if (!matched) {
                    break;
                }
            }

            // Return the length of the invalid excess
            // if we're just parsing
            // Otherwise, throw an error or return tokens
            return parseOnly ?
                soFar.length :
                soFar ?
                Sizzle.error(selector) :
            // Cache the tokens
            tokenCache(selector, groups).slice(0);
        }

猜你喜欢

转载自blog.csdn.net/lay136362687/article/details/80384502