heftykoo / reading-zepto Goto Github PK
View Code? Open in Web Editor NEW读 Zepto 源码,分析 Zepto 源码
License: Other
读 Zepto 源码,分析 Zepto 源码
License: Other
var h = $('h3'); Array.isArray(h) === true;
element.nodeType !== 1 && element.nodeType !== 9 && element.nodeType !== 11 。
1 对应的是 Node.ELEMENT_NODE ,10 对应的是 Node.DOCUMENT_TYPE_NODE , 11 对应的是 Node.DOCUMENT_FRAGMENT_NODE ,如果不为以上三种类型,直接返回 []。
这里楼主写错了 10 对应的是 Node.DOCUMENT_TYPE_NODE, 应该是 9对应 着Node.DOCUMENT_NODE
function likeArray(obj) {
console.log(type,$.type);
var length = !!obj && length in obj && obj.length,
type = type(obj); // 这里重新定义了变量type将上文定义的type函数替换,'='后面的type(obj)会报错:type不是函数
}
我认为是这样的
zepto.init = function(selector, context) {
var dom // dom 集合
if (!selector) return zepto.Z() // 分支1
else if (typeof selector == 'string') { // 分支2
selector = selector.trim()
if (selector[0] == '<' && fragmentRE.test(selector))
dom = zepto.fragment(selector, RegExp.$1, context), selector = null
else if (context !== undefined) return $(context).find(selector)
else dom = zepto.qsa(document, selector)
}
else if (isFunction(selector)) return $(document).ready(selector) // 分支3
else if (zepto.isZ(selector)) return selector // 分支4
else { // 分支5
if (isArray(selector)) dom = compact(selector)
else if (isObject(selector))
dom = [selector], selector = null
else if (fragmentRE.test(selector))
dom = zepto.fragment(selector.trim(), RegExp.$1, context), selector = null
else if (context !== undefined) return $(context).find(selector)
else dom = zepto.qsa(document, selector)
}
return zepto.Z(dom, selector)
}
分支2 是 selector 是字符串的情况下处理 zepto.fragment()
的, 像 $('<div>')
分支5 引用类型下,不是数组,不是对象,未知名引用自动转为 html 调用 zepto.fragment()
的, 我也具不出拿例子, 或者是 $(zepto collection) ?
但添加了还是有作用的,逻辑上面的严谨.
感觉在 type = $.type(obj) 这里 ,
把
和for 里面的len感觉是一样
for(var i = 0, len = array.lenght;i<len; i++) {
...
}
len 单独等出来,使每次循环的时候不用遍历数组计算长度.应该是一个道理啊.
zepto 的核心是一个闭包,加载完毕后立即执行。然后暴露给全局变量 zepto ,如果 $ 没有定义,也将 $ 赋值为 zepto 。
---> 第二个zepto
应该是Zepto
吧
在数据类型检测——isArray处:
我们都知道,instanceof 的检测的原理是查找实例的 prototype 是否在构造函数的原型链上,如果在,则返回 true。
是不是应该修改为:
我们都知道,instanceof 的检测的原理是查找构造函数的 prototype 是否在实例的原型链上,如果在,则返回 true。
即 a instanceof b
,是查找 b.prototype 是否在 a 的原型链上。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.