Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

读Zepto源码之神奇的$ 的 zepto.init 下存在 两个 html 片段处理的情况 #6

Open
ClarenceC opened this issue Jan 16, 2018 · 1 comment
Labels

Comments

@ClarenceC
Copy link

ClarenceC commented Jan 16, 2018

我认为是这样的

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) ?
但添加了还是有作用的,逻辑上面的严谨.

@fangbinwei
Copy link

fangbinwei commented Feb 11, 2018

hi @ClarenceC 我个人觉得是针对 $(new String/Object('<p>example</p>'))这种情况, 虽然这种情况不太常见.

typeof new String('<p>example</p>')// object 走分支5
type(new String('<p>example</p>')) // string 
isObject(new String('<p>example</p>')) //false

isObject() return false的原因

 $.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) {
    class2type[ "[object " + name + "]" ] = name.toLowerCase()
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants