做网站需要多少费用,简单大气网站,百度推广服务,游戏网站开发难度Virtual DOM 这个概念相信大部分人都不会陌生#xff0c;它产生的前提是浏览器中的 DOM 是很“昂贵的#xff0c;为了更直观的感受#xff0c;我们可以简单的把一个简单的 div 元素的属性都打印出来#xff0c;如图所示#xff1a; 可以看到#xff0c;真正的 DOM …Virtual DOM 这个概念相信大部分人都不会陌生它产生的前提是浏览器中的 DOM 是很“昂贵的为了更直观的感受我们可以简单的把一个简单的 div 元素的属性都打印出来如图所示 可以看到真正的 DOM 元素是非常庞大的因为浏览器的标准就把 DOM 设计的非常复杂。当我们频繁的去做 DOM 更新会产生一定的性能问题。
而 Virtual DOM 就是用一个原生的 JS 对象去描述一个 DOM 节点所以它比创建一个 DOM 的代价要小很多。在 Vue.js 中Virtual DOM 是用 VNode 这么一个 Class 去描述它是定义在 src/core/vdom/vnode.js 中的。
export default class VNode {tag: string | void;data: VNodeData | void;children: ?ArrayVNode;text: string | void;elm: Node | void;ns: string | void;context: Component | void; // rendered in this components scopekey: string | number | void;componentOptions: VNodeComponentOptions | void;componentInstance: Component | void; // component instanceparent: VNode | void; // component placeholder node// strictly internalraw: boolean; // contains raw HTML? (server only)isStatic: boolean; // hoisted static nodeisRootInsert: boolean; // necessary for enter transition checkisComment: boolean; // empty comment placeholder?isCloned: boolean; // is a cloned node?isOnce: boolean; // is a v-once node?asyncFactory: Function | void; // async component factory functionasyncMeta: Object | void;isAsyncPlaceholder: boolean;ssrContext: Object | void;fnContext: Component | void; // real context vm for functional nodesfnOptions: ?ComponentOptions; // for SSR cachingfnScopeId: ?string; // functional scope id supportconstructor(tag?: string,data?: VNodeData,children?: ?ArrayVNode,text?: string,elm?: Node,context?: Component,componentOptions?: VNodeComponentOptions,asyncFactory?: Function) {this.tag tagthis.data datathis.children childrenthis.text textthis.elm elmthis.ns undefinedthis.context contextthis.fnContext undefinedthis.fnOptions undefinedthis.fnScopeId undefinedthis.key data data.keythis.componentOptions componentOptionsthis.componentInstance undefinedthis.parent undefinedthis.raw falsethis.isStatic falsethis.isRootInsert truethis.isComment falsethis.isCloned falsethis.isOnce falsethis.asyncFactory asyncFactorythis.asyncMeta undefinedthis.isAsyncPlaceholder false}// DEPRECATED: alias for componentInstance for backwards compat.// istanbul ignore nextget child(): Component | void {return this.componentInstance}
}
可以看到 Vue.js 中的 Virtual DOM 的定义还是略微复杂一些的因为它这里包含了很多 Vue.js 的特性。这里千万不要被这些茫茫多的属性吓到实际上 Vue.js 中 Virtual DOM 是借鉴了一个开源库 snabbdom 的实现然后加入了一些 Vue.js 特色的东西。建议大家如果想深入了解 Vue.js 的 Virtual DOM 前不妨先阅读这个库的源码因为它更加简单和纯粹。
总结
其实 VNode 是对真实 DOM 的一种抽象描述它的核心定义无非就几个关键属性标签名、数据、子节点、键值等其它属性都是用来扩展 VNode 的灵活性以及实现一些特殊 feature 的。由于 VNode 只是用来映射到真实 DOM 的渲染不需要包含操作 DOM 的方法因此它是非常轻量和简单的。
Virtual DOM 除了它的数据结构的定义映射到真实的 DOM 实际上要经历 VNode 的 create、diff、patch 等过程。那么在 Vue.js 中VNode 的 create 是通过之前提到的 createElement 方法创建的我们接下来分析这部分的实现。 文章转载自: http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.bfjtp.cn.gov.cn.bfjtp.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.mdlqf.cn.gov.cn.mdlqf.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.lkgqb.cn.gov.cn.lkgqb.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.pphbn.cn.gov.cn.pphbn.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.zlrrj.cn.gov.cn.zlrrj.cn http://www.morning.cjmmt.cn.gov.cn.cjmmt.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.mpnff.cn.gov.cn.mpnff.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.hrzky.cn.gov.cn.hrzky.cn http://www.morning.tgwfn.cn.gov.cn.tgwfn.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.tgmwy.cn.gov.cn.tgmwy.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.gzttoyp.com.gov.cn.gzttoyp.com http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.bgrsr.cn.gov.cn.bgrsr.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.rtbhz.cn.gov.cn.rtbhz.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.rpms.cn.gov.cn.rpms.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.qmkyp.cn.gov.cn.qmkyp.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.lxqyf.cn.gov.cn.lxqyf.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn