jQuery源码分析之init的详细介绍

2020-05-23 06:16:37易采站长站整理
parsed = buildFragment([data], context, scripts);
建立好一个 fragment 对象,用
parsed.childNodes 
来获取这些 data 对应的 HTML。

jQueyr.makeArray

jQuery 里面的函数调用,真的是一层接一层,虽然有时候光靠函数名,就能知道这函数的作用,但其中思考之逻辑还是挺参考意义的。


jQuery.makeArray = function (arr, results) {
var ret = results || [];

if (arr != null) {
if (isArrayLike(Object(arr))) {
jQuery.merge(ret, typeof arr === "string" ? [arr] : arr);
} else {
push.call(ret, arr);
}
}

return ret;
}

makeArray 把左边的数组或字符串并入到右边的数组或一个新数组,其中又间接的引用

 jQuery.merge 
函数。

接下来是着 isArrayLike 函数,可能需要考虑多方面的因素,比如兼容浏览器等,就有了下面这一长串:


function isArrayLike(obj) {

// Support: real iOS 8.2 only (not reproducible in simulator)
// `in` check used to prevent JIT error (gh-2145)
// hasOwn isn't used here due to false negatives
// regarding Nodelist length in IE
var length = !!obj && "length" in obj && obj.length,
type = jQuery.type(obj);

if (type === "function" || jQuery.isWindow(obj)) {
return false;
}

return type === "array" || length === 0 || typeof length === "number" && length > 0 && (length - 1) in obj;
}

总结

这篇文章主要介绍了jQuery中比较重要的入口函数,之后将会继续讲解 Sizzle,jQuery 中的选择器。感兴趣的朋友们请继续关注软件开发网,谢谢大家的支持。