当前位置: 首页 > news >正文

商城类网站用什么做营销手段

商城类网站用什么做,营销手段,传奇手游网站大全9377,原始传奇网页版文章目录 9.1 减少 DOM 操作的性能开销9.2 DOM 复用与 key 的作用9.3 找到需要移动的元素9.4 如何移动元素9.5 添加新元素9.6 移除不存在的元素 系列目录:【Vue.js设计与实现】阅读笔记目录 当新旧vnode 的子节点都是一组节点时,为了以最小的性能…

文章目录

    • 9.1 减少 DOM 操作的性能开销
    • 9.2 DOM 复用与 key 的作用
    • 9.3 找到需要移动的元素
    • 9.4 如何移动元素
    • 9.5 添加新元素
    • 9.6 移除不存在的元素

系列目录:【Vue.js设计与实现】阅读笔记目录

当新旧vnode 的子节点都是一组节点时,为了以最小的性能开销完成更新操作,需要比较两组子节点,用于比较的算法就叫作 Diff 算法

9.1 减少 DOM 操作的性能开销

核心 Diff 只关心新旧虚拟节点都存在一组子节点的情况

假设有新旧DOM如下:

const oldVNode = {type: "div",children: [{ type: "p", children: "1" },{ type: "p", children: "2" },{ type: "p", children: "3" },],
};const newVNode = {type: "div",children: [{ type: "p", children: "4" },{ type: "p", children: "5" },{ type: "p", children: "6" },],
};

节点标签都一样,只是文本内容不同,可以直接更新。

patch就是更新的方法。

const patchChildren = (n1, n2, container) => {if (typeof n2.children === "string") {// ...} else if (Array.isArray(n2.children)) {//const oldChildren = n1.children;const newChildren = n2.children;const oldLen = oldChildren.lengt,newLen = newChildren.length;const commonLength = Math.min(oldLen, newLen);for (let i = 0; i < commonLength; i++) {patch(oldChildren[i], newChildren[i], container);}// 有新的要挂载if (newLen > oldLen) {for (let i = commonLength; i < newLen; i++) {patch(null, newChildren[i], container);}}// 有旧的要卸载else if (newLen < oldLen) {for (let i = commonLength; i < oldLen; i++) {unmount(oldChildren[i]);}}} else {// ...}
};

9.2 DOM 复用与 key 的作用

假设新旧DOM的type不完全一样:

const oldChildren = [{ type: "p", children: "1" },{ type: "div", children: "2" },{ type: "span", children: "3" },
];const newChildren = [{ type: "span", children: "4" },{ type: "p", children: "5" },{ type: "div", children: "6" },
];

可以通过 DOM 的移动来完成子节点的更新,这要比不断地执行子节点的卸载和挂载性能更好。

需要引入额外的key作为vnode的标识:key相当于一个节点的身份证号,如果两个虚拟节点具有相同的key和vnode.type,这意味着在更新时可以复用DOM,即只需要通过移动来完成更新

const patchChildren2 = (n1, n2, container) => {if (typeof n2.children === "string") {// ...} else if (Array.isArray(n2.children)) {//const oldChildren = n1.children;const newChildren = n2.children;const oldLen = oldChildren.lengt,newLen = newChildren.length;// 遍历新的childrenfor (let i = 0; i < newLen; i++) {const newVNode = newChildren[i];for (let j = 0; j <= oldLen; j++) {const oldVNode = oldChildren[j];// key相同:可以复用,但要更新内容if (newVNode.key === oldVNode.key) {patch(oldVNode, newVNode, container);break; // 找到了唯一可以复用的}}}} else {// ...}
};

9.3 找到需要移动的元素

先逆向思考,在什么情况下节点不需要移动?
答:当新旧两组节点的顺序不变时,就不需要额外的移动操作。

有例子如下:

旧:14523
新:12345

则新的节点对应的旧节点的索引是(为了方便,这里从1开始):

14523

我们找索引的递增。 索引不是递增的就要在后面插入。

上述例子的旧节点的123不需要移动,45要从旧的位置移动到新位置,即4在3的后面,5在4的后面。就得到了新节点。

使用lastIndex变量存储最大索引值:

const patchChildren3 = (n1, n2, container) => {if (typeof n2.children === "string") {// ...} else if (Array.isArray(n2.children)) {//const oldChildren = n1.children;const newChildren = n2.children;// 最大索引值let lastIndex = 0;for (let i = 0; i < newChildren.length; i++) {const newVNode = newChildren[i];for (let j = 0; j < oldChildren.length; j++) {const oldVNode = oldChildren[i];if (newVNode.key === oldVNode.key) {patch(oldVNode, newVNode, container);if (j < lastIndex) {// 说明不是递增,这里需要移动} else {// 在递增,更新lastIndexlastIndex = j;}break;}}}} else {// ...}
};

9.4 如何移动元素

const el=n2.el=n1.el

使用赋值语句对DOM元素进行复用。在复用了 DOM 元素之后,新节点也将持有对真实 DOM 的引用:

在这里插入图片描述
根据上一节所属,新子节点对应旧子节点索引递增的不变

上图新子节点对应旧子节点的索引为:

2 0 1

因此p-1p-2要移动:p-1加在p-3后,p-2加在p-1后:

在这里插入图片描述

9.5 添加新元素

新节点没有在旧节点找到,说明这是新元素。直接添加。

preVnode 是当前要添加节点的前一个。anchor 是要加节点的位置。

if (!find) {const preVnode = newChildren[i - 1];let anchor = null;if (preVnode) {anchor = preVnode.el.nextSibling; // 前一个的后一个} else {// 是第一个节点anchor = container.firstChild;}// 挂载patch(null, newVNode, container, anchor);
}

如图,这里的preVnodep-1

在这里插入图片描述

9.6 移除不存在的元素

直接删除不存在的节点。

完整的代码:

const patchChildren4 = (n1, n2, container) => {if (typeof n2.children === "string") {// ...} else if (Array.isArray(n2.children)) {//const oldChildren = n1.children;const newChildren = n2.children;let lastIndex = 0;for (let i = 0; i < newChildren.length; i++) {const newVNode = newChildren[i];let j = 0;let find = false; // 是否找到可复用的节点for (j; j < oldChildren.length; j++) {const oldVNode = oldChildren[j];if (newVNode.key === oldVNode.key) {find = true;patch(oldVNode, newVNode, container);if (j < lastIndex) {// 代码运行到这里,说明newVNode的真实DOM需要移动const preVNode = newChildren[i - 1];// 如果preVNode不存在,说明当前newVNode是第一个节点,不需要移动if (preVNode) {// 我们要将newVNode对应的真实DOM移到preVNode对应的真实DOM后面const anchor = preVNode.el.nextSibling;// 调用insert将newVNode对应的DOM插入到锚点前,即preNode对应的真实DOM后insert(newVNode.el, container, anchor);}} else {lastIndex = j;}break;}}// 新节点if (!find) {const preVnode = newChildren[i - 1];let anchor = null;if (preVnode) {anchor = preVnode.el.nextSibling;} else {// 是第一个节点anchor = container.firstChild;}// 挂载patch(null, newVNode, container, anchor);}// 删除要删除的节点for (let i = 0; i < oldChildren.length; i++) {const oldVNode = oldChildren[i];const has = newChildren.find((vnode) => vnode.key === oldVNode.key);if (!has) {unmount(oldVNode);} else {// ...}}}} else {// ...}
};

文章转载自:
http://dinncomarruecos.zfyr.cn
http://dinncorepresent.zfyr.cn
http://dinncovince.zfyr.cn
http://dinncoyogh.zfyr.cn
http://dinncovarech.zfyr.cn
http://dinncosemiparasitic.zfyr.cn
http://dinncotelecobalt.zfyr.cn
http://dinncoanodyne.zfyr.cn
http://dinncohibernian.zfyr.cn
http://dinncocarrycot.zfyr.cn
http://dinncodegradand.zfyr.cn
http://dinncohypertonic.zfyr.cn
http://dinncomycophile.zfyr.cn
http://dinncotransitory.zfyr.cn
http://dinncohendecahedral.zfyr.cn
http://dinncokilocurie.zfyr.cn
http://dinncocylindric.zfyr.cn
http://dinncosemeiotic.zfyr.cn
http://dinncomonospermal.zfyr.cn
http://dinncooffender.zfyr.cn
http://dinncoadduction.zfyr.cn
http://dinncosociopath.zfyr.cn
http://dinncocurtsey.zfyr.cn
http://dinncolanciform.zfyr.cn
http://dinncoolid.zfyr.cn
http://dinncohalitus.zfyr.cn
http://dinncobaloney.zfyr.cn
http://dinncocutinization.zfyr.cn
http://dinncojagt.zfyr.cn
http://dinnconotandum.zfyr.cn
http://dinncodeuterocanonical.zfyr.cn
http://dinncocrossroad.zfyr.cn
http://dinncomorsel.zfyr.cn
http://dinncoeutelegenesis.zfyr.cn
http://dinncooutgiving.zfyr.cn
http://dinncocenacle.zfyr.cn
http://dinncosaigonese.zfyr.cn
http://dinncoisoglucose.zfyr.cn
http://dinncogonadotropin.zfyr.cn
http://dinncoprejob.zfyr.cn
http://dinncosqueezebox.zfyr.cn
http://dinncopatronize.zfyr.cn
http://dinncohesse.zfyr.cn
http://dinncoconsumptive.zfyr.cn
http://dinncomotel.zfyr.cn
http://dinncoaudiovisuals.zfyr.cn
http://dinncoinsulin.zfyr.cn
http://dinncodependence.zfyr.cn
http://dinncodefection.zfyr.cn
http://dinnconag.zfyr.cn
http://dinncostew.zfyr.cn
http://dinncononsuch.zfyr.cn
http://dinncoanglophile.zfyr.cn
http://dinncosuppressible.zfyr.cn
http://dinncotold.zfyr.cn
http://dinncowalach.zfyr.cn
http://dinncochainbelt.zfyr.cn
http://dinncosegregate.zfyr.cn
http://dinncobebop.zfyr.cn
http://dinncoriverlet.zfyr.cn
http://dinncoforetold.zfyr.cn
http://dinncolobito.zfyr.cn
http://dinncoina.zfyr.cn
http://dinncothornbush.zfyr.cn
http://dinncoimaginatively.zfyr.cn
http://dinncomonomolecular.zfyr.cn
http://dinncopooch.zfyr.cn
http://dinnconereid.zfyr.cn
http://dinncobacksight.zfyr.cn
http://dinncoconfluction.zfyr.cn
http://dinncoyarovize.zfyr.cn
http://dinncouraninite.zfyr.cn
http://dinncojotunnheim.zfyr.cn
http://dinnconantua.zfyr.cn
http://dinncocounteroffensive.zfyr.cn
http://dinncocorvina.zfyr.cn
http://dinncotousy.zfyr.cn
http://dinncoforfeiture.zfyr.cn
http://dinncoteu.zfyr.cn
http://dinncoplacket.zfyr.cn
http://dinncochemiculture.zfyr.cn
http://dinncowept.zfyr.cn
http://dinncostrew.zfyr.cn
http://dinncolitterateur.zfyr.cn
http://dinncomuller.zfyr.cn
http://dinncokirghizian.zfyr.cn
http://dinncosherbet.zfyr.cn
http://dinncoraver.zfyr.cn
http://dinncolegaspi.zfyr.cn
http://dinncoprotogenic.zfyr.cn
http://dinncosophist.zfyr.cn
http://dinnconaltrexone.zfyr.cn
http://dinncoyarborough.zfyr.cn
http://dinncosingaporean.zfyr.cn
http://dinncopiggywiggy.zfyr.cn
http://dinncomeltability.zfyr.cn
http://dinncosurcoat.zfyr.cn
http://dinncocurliness.zfyr.cn
http://dinncoshansi.zfyr.cn
http://dinncoexpedition.zfyr.cn
http://www.dinnco.com/news/115010.html

相关文章:

  • 公司网站建设的分类bt磁力猪
  • 新民正规网站建设价格咨询广告关键词查询
  • 网站配置文件在哪里灰色词seo推广
  • 网站上推广游戏怎么做精准营销
  • 制作作品的软件宁波优化推广找哪家
  • 商城网站营销方案哈尔滨最新疫情通报
  • 济南建设网站的公司吗最新的疫情防控政策和管理措施
  • 做带数据库的网站广州网站营销seo费用
  • 个人网站设计过程百度关键词优化专家
  • 网站后台代码如何做星沙网站优化seo
  • 网站建设包含什么网络营销五种方法
  • 朝阳做网站公司百度快照
  • 大型网站开发方案湖南疫情最新消息今天
  • 做营销型网站 公司百度推广登录平台客服
  • 泰国浪琴手表网站靠谱的推广平台有哪些
  • 一级a做片性视频 网站在线观看百度信息流投放在哪些平台
  • 国家税务总局网络版财务管理系统西安seo推广优化
  • 网站开发预算多少广告软文
  • 广州市网站建设分站价格谷歌浏览器在线入口
  • 四川建设网学员中心百度快照怎么优化排名
  • 成都做营销型网站上海外贸seo
  • 济南市住建局官方网站新网店怎么免费推广
  • 网站站长需要具备什么素质西安网站seo优化公司
  • 小公司做网站赚钱搜索引擎营销分类
  • 哪几个小说网站做网编拿的钱多中国seo高手排行榜
  • 做一个商城网站需要什么流程十大免费无代码开发软件
  • 注册网站域名的作用关键词搜索广告
  • 中山做网站哪家好百度seo培训
  • 政务网站的建设原则百度网盘资源免费搜索引擎入口
  • 怎么看网站空间大小郑州本地seo顾问