vue源码中的createElement

vue源码中有一个点不懂(在createElementNS函数中)
document.createElement(namespace + ':' + tagName),第一次见到会带冒号的
查了一下MDN上的createElement,然后发现不能这样写:

MDN上的createElement解释

不过根据这张图和下面的constructor里面的this.ns = undefined,貌似是要解决其他文档的兼容性的感觉

MDN createElement开头说明
export default class VNode {
  // 这里省略一堆属性
  constructor (
    tag?: string,
    data?: VNodeData,
    children?: ?Array<VNode>,
    text?: string,
    elm?: Node,
    context?: Component,
    componentOptions?: VNodeComponentOptions,
    asyncFactory?: Function
  ) {
    this.tag = tag
    this.data = data
    this.children = children
    this.text = text
    this.elm = elm
    this.ns = undefined // constructor这里一开始把ns设置成了undefined,看起来上面的那个问题应该是为了兼容其他文档
    this.context = context
    this.fnContext = undefined
    this.fnOptions = undefined
    this.fnScopeId = undefined
    this.key = data && data.key
    this.componentOptions = componentOptions
    this.componentInstance = undefined
    this.parent = undefined
    this.raw = false
    this.isStatic = false
    this.isRootInsert = true
    this.isComment = false
    this.isCloned = false
    this.isOnce = false
    this.asyncFactory = asyncFactory
    this.asyncMeta = undefined
    this.isAsyncPlaceholder = false
  }

  // DEPRECATED: alias for componentInstance for backwards compat.
  /* istanbul ignore next */
  get child (): Component | void {
    return this.componentInstance
  }
}

不过实际上也有一个可以指定元素名称空间的createElementNS函数, 但是vue 2.0的诞生时间可能比这个函数早, 所以需要这样写

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。