{"id":274,"date":"2021-07-15T13:38:23","date_gmt":"2021-07-15T05:38:23","guid":{"rendered":"http:\/\/gjweb.top\/?p=274"},"modified":"2021-07-15T13:38:24","modified_gmt":"2021-07-15T05:38:24","slug":"%e8%99%9a%e6%8b%9fdom-%e5%92%8c-diff-%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=274","title":{"rendered":"\u865a\u62dfDOM \u548c diff \u7b97\u6cd5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u865a\u62dfDOM<\/h2>\n\n\n\n<p>\u7528JS\u5bf9\u8c61\u63cf\u8ff0DOM\u7684\u5c42\u6b21\u7ed3\u6784\uff0cDOM\u4e2d\u7684\u4e00\u5207\u5c5e\u6027\u90fd\u5728\u865a\u62dfDOM\u4e2d\u6709\u5bf9\u5e94\u7684\u5c5e\u6027<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ef\u4ee5\u51cf\u5c11DOM\u64cd\u4f5c\u3002\u865a\u62dfDOM\u53ef\u4ee5\u5c06\u591a\u6b21\u64cd\u4f5c\u5408\u5e76\u6210\u4e00\u6b21\u64cd\u4f5c\uff0c\u5e76\u4e14\u501f\u52a9DIFF\u7b97\u6cd5\u628a\u591a\u4f59\u7684\u64cd\u4f5c\u7701\u6389<\/li><li>\u8de8\u5e73\u53f0\u3002\u865a\u62dfDOM\u4e0d\u4ec5\u53ef\u4ee5\u53d8\u6210DOM\uff0c\u8fd8\u53ef\u53d8\u6210\u5c0f\u7a0b\u5e8f\u3001ios\u3001\u5b89\u5353\u5e94\u7528\uff0c\u56e0\u4e3a\u4ed6\u672c\u8d28\u662f\u4e00\u4e2aJS\u5bf9\u8c61<\/li><\/ul>\n\n\n\n<p>\u5982\u4e0b\uff1asanbbdom\u7684\u865a\u62dfDOM\u6837\u5f0f (sanbbdom\u662f\u6700\u65e9\u7684\u865a\u62dfDOM\u548cdiff\u7b97\u6cd5\u7684\u524d\u7aef\u5e93,\u540e\u671f\u7684React\u548cVue\u4e5f\u662f\u501f\u9274\u4e86sanbbdom\u6765\u5b9e\u73b0\u7684\u865a\u62dfDOM)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/z3.ax1x.com\/2021\/07\/14\/WVFaLV.md.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/z3.ax1x.com\/2021\/07\/14\/WVFaLV.md.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"WVFaLV.md.png\"\/><\/div><\/figure>\n\n\n\n<p><a href=\"https:\/\/imgtu.com\/i\/WVFaLV\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">React \u548c Vue \u7684\u865a\u62dfDOM\u662f\u4ec0\u4e48\u6837\u5b50\u7684\uff1f<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ React \u7684 \u865a\u62dfdom<br>const vNode = {<br> &nbsp; &nbsp;key : null,<br> &nbsp; &nbsp;props:{<br> &nbsp; &nbsp; &nbsp; &nbsp;children:&#91; \/\/ \u5b50\u5143\u7d20<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {type:'span',...},<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  {type:'span',...}<br> &nbsp; &nbsp; &nbsp;  ],<br> &nbsp; &nbsp; &nbsp; &nbsp;className:\"red\", \/\/ \u6807\u7b7e\u7684class\u5c5e\u6027<br> &nbsp; &nbsp; &nbsp; &nbsp;onClick:()=&gt;{}, \/\/ \u4e8b\u4ef6<br> &nbsp;  },<br> &nbsp; &nbsp;ref: null,<br> &nbsp; &nbsp;type: \"div\", \/\/ \u6807\u7b7e \u6216 \u7ec4\u4ef6\u540d<br> &nbsp; &nbsp;...<br>}<br>\/\/ Vue \u7684 \u865a\u62dfDOM<br>const vNode = {<br> &nbsp; &nbsp;tag :'div', \/\/ \u6807\u7b7e \u6216 \u7ec4\u4ef6\u540d<br> &nbsp; &nbsp;data:{<br> &nbsp;      class:\"red\", \/\/ \u6807\u7b7e\u7684class\u5c5e\u6027<br> &nbsp;      on:{<br> &nbsp;          click:()=&gt;{} \/\/ \u4e8b\u4ef6<br>        }<br>    },<br> &nbsp; &nbsp;children:&#91; \/\/ \u5b50\u5143\u7d20<br> &nbsp; &nbsp; &nbsp;  {tag:'span',...},<br> &nbsp; &nbsp; &nbsp;  {tag:\"span\",...}<br> &nbsp;  ]<br>    ...<br>}<\/code><\/pre>\n\n\n\n<p>\u95ee\u9898: \u865a\u62dfDOM\u662f\u5982\u4f55\u4ea7\u751f\u7684\u5462?<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5982\u4f55\u521b\u5efa\u865a\u62dfDOM<\/h3>\n\n\n\n<p>\u5728 React \u4e2d\u6846\u67b6\u63d0\u4f9b\u4e86 createElement API<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>createElement('div',{className:'red',onClick:()=&gt;{}},&#91;<br> &nbsp; &nbsp;createElement(\"span\",{},\"\u5b50\u5143\u7d20\u4e00\")<br> &nbsp; &nbsp;createElement(\"span\",{},\"\u5b50\u5143\u7d20\u4e8c\")<br>])<br>\/\/ react\u63d0\u4f9b\u4e86JSX\u7b80\u5316\u865a\u62dfDOM\u7684\u521b\u5efa , \u901a\u8fc7 Babel \u8f6c\u5316\u4e3a \u4e0a\u9762\u7684\u5199\u6cd5 (babel\u76f4\u63a5\u5185\u7f6e\u4e86JSX\u8bed\u6cd5\u89e3\u6790)<br>&lt;div className=\"red\" onClick=\"{()=&gt;{}}\"&gt;<br>    &lt;span&gt;\u5b50\u5143\u7d20\u4e00&lt;\/span&gt;<br>    &lt;span&gt;\u5b50\u5143\u7d20\u4e8c&lt;\/span&gt;<br>&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>\u5728 Vue \u4e2d\u6846\u67b6\u63d0\u4f9b\u4e86 h \u51fd\u6570 (\u53ea\u80fd\u5728 render \u51fd\u6570\u91cc\u5f97\u5230 h )<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>h(\"div\",{<br> &nbsp; &nbsp;class:'red',<br> &nbsp; &nbsp;on:{<br> &nbsp; &nbsp; &nbsp; &nbsp;click:()=&gt;{}<br> &nbsp;  },<br> &nbsp;  &#91;<br> &nbsp;      h('span',{},'\u5b50\u5143\u7d20'),<br> &nbsp;      h('span',{},'\u5b50\u5143\u7d20\u4e8c')<br> &nbsp;  ]<br>})<br>\/\/ Vue \u63d0\u4f9b\u4e86 Tmplate \u6a21\u677f\u8bed\u6cd5; \u901a\u8fc7 vue-loader \u8f6c\u5316\u4e3a \u4e0a\u9762\u7684\u5199\u6cd5<br>&lt;div calss=\"red\" @click=\"fn\"&gt;<br>    &lt;span&gt;\u5b50\u5143\u7d20&lt;\/span&gt;<br>    &lt;span&gt;\u5b50\u5143\u7d20\u4e8c&lt;span&gt;<br>&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">diff\u7b97\u6cd5<\/h2>\n\n\n\n<p>\u65b0\u865a\u62dfDOM\u548c\u8001\u865a\u62dfDOM\u8fdb\u884cdiff\uff08\u7cbe\u7ec6\u5316\u6bd4\u8f83\uff09\uff0c \u7b97\u51fa\u5e94\u8be5\u5982\u4f55\u6700\u5c0f\u91cf\u66f4\u65b0\uff0c\u6700\u540e\u53cd\u6620\u5230\u771f\u6b63\u7684DOM\u4e0a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">diff\u7684\u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u53ea\u6709\u662f\u540c\u4e00\u4e2a\u865a\u62df\u8282\u70b9,\u624d\u4f1a\u8fdb\u884c\u7cbe\u7ec6\u5316\u6bd4\u8f83,\u5426\u5219\u5c31\u662f\u66b4\u529b\u5220\u9664,\u91cd\u5efa<ul><li>\u5982\u4f55\u5b9a\u4e49\u662f\u540c\u4e00\u4e2a\u865a\u62df\u8282\u70b9?<ul><li>\u9009\u62e9\u5668\u76f8\u540c\u4e14key\u76f8\u540c<\/li><\/ul><\/li><\/ul><\/li><li>\u53ea\u8fdb\u884c\u540c\u5c42\u6bd4\u8f83,\u4e0d\u4f1a\u8fdb\u884c\u8de8\u5c42\u6bd4\u8f83,\u5373\u4f7f\u662f\u540c\u4e00\u7247\u865a\u62df\u8282\u70b9,\u4f46\u662f\u8de8\u5c42\u4e86\u4e5f\u4f1a\u8fdb\u884c\u66b4\u529b\u5220\u9664\u548c\u91cd\u5efa<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">snabbdom\u4f7f\u7528<\/h2>\n\n\n\n<p>snabbdom \u662f\u65e9\u8d77\u7684\u524d\u7aef\u865a\u62dfDOM\u5e93, Vue\u3001React\u7b49\u6846\u67b6\u7684\u865a\u62dfDOM\u548cDIFF\u7b97\u6cd5\u4e5f\u662f\u501f\u9274\u4e86sanbbdom\u7684\u601d\u60f3<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -D sanbbdom<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import {<br> &nbsp;init,<br> &nbsp;classModule,<br> &nbsp;propsModule,<br> &nbsp;styleModule,<br> &nbsp;eventListenersModule,<br> &nbsp;h,<br>} from \"snabbdom\";<br>const patch = init(&#91;classModule,<br> &nbsp;propsModule,<br> &nbsp;styleModule,<br> &nbsp;eventListenersModule])<br>\u200b<br>const myVnode1 = h(\"a\",{<br> &nbsp; &nbsp;props:{<br> &nbsp; &nbsp; &nbsp; &nbsp;href:\"http:\/\/www.gjweb.top\"<br> &nbsp;  }<br>},\"\u70b9\u51fb\u8df3\u8f6c\")<br>\u200b<br>const container = document.getElementById(\"container\")<br>patch(container,myVnode1)<br>\u200b<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u865a\u62df\u8282\u70b9<\/h3>\n\n\n\n<p>h\u51fd\u6570\u7528\u6765\u4ea7\u751f\u865a\u62df\u8282\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ h\u51fd\u6570<br>h('a',{props:{href:'http:\/\/gjweb.top.com'}},'\u535a\u5ba2\u5730\u5740')<br>\/\/ \u5f97\u5230\u7684\u865a\u62df\u8282\u70b9<br>{\"sel\":\"a\",\"data\":{props:{href:\"http:\/\/gjweb.top\"}},\"text\":'\u535a\u5ba2\u5730\u5740'}<br>\/\/ \u771f\u6b63\u7684DOM\u8282\u70b9<br>&lt;a href='http:\/\/gjweb.top'&gt;\u535a\u5ba2\u5730\u5740&lt;\/a&gt;<br>\u200b<\/code><\/pre>\n\n\n\n<p>\u865a\u62df\u8282\u70b9\u6709\u54ea\u4e9b\u5c5e\u6027<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{<br> &nbsp; &nbsp;children:{} \/\/ \u8868\u793a\u4e2a\u5143\u7d20\u4e0b\u7684\u5b50\u5143\u7d20\u7684\u8282\u70b9\u5c5e\u6027<br> &nbsp; &nbsp;data:{} \/\/ \u8868\u793a\u5f53\u524d\u8282\u70b9\u7684\u5404\u4e2a\u5143\u7d20<br> &nbsp; &nbsp;elm\uff1a\/\/ \u8868\u793a\u771f\u5b9eDOM\u7684\u5730\u5740,\u5982\u679c\u662fundefined\u8868\u793a\u6ca1\u6709\u4e0a\u6811<br> &nbsp; &nbsp;key\uff1a \/\/ \u865a\u62df\u8282\u70b9\u7684\u552f\u4e00\u6807\u8bc6<br> &nbsp; &nbsp;sel: \/\/ \u9009\u62e9\u5668<br> &nbsp; &nbsp;text\uff1a \/\/ \u6587\u5b57<br> &nbsp; &nbsp;<br>}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">h\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u65b9\u5f0f\u4e00<br>h(\"div\",{},\"hello world\")<br>\/\/ \u65b9\u5f0f\u4e8c<br>h(\"ul\",{},&#91;<br> &nbsp; &nbsp;h(\"li\",{},\"item1\"),<br> &nbsp; &nbsp;h(\"li\",{},\"item2\"),<br> &nbsp; &nbsp;<br>])<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7b80\u5355\u4ecb\u7ecd\u4e86\u4e00\u4e0b \u865a\u62dfDOM\u7684\u521b\u5efa\u53ca\u4f7f\u7528\uff0c\u4e0b\u9762\u7b80\u5355\u5b9e\u73b0\u4e0bh\u7684\u6267\u884c\u903b\u8f91<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9e\u73b0\u7b80\u5355\u7684h\u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*<br>* \u4ec5\u652f\u6301<br>* h(\"div\",{},\"hello\")<br>* h(\"div\",{},&#91;h(\"span\",{},\"hello\"),h(\"p\",{},\"world\")])<br>* h(\"ul\",{},h(\"li\",{},\"hello world\"))<br>*\/<br>function vnode(sel, data, children, text, elm){<br>  return {<br>    sel,<br>    data,<br>    children,<br>    text,<br>    elm<br>  }<br>}<br>export default function(sel,data,c){<br>  \/\/ \u68c0\u67e5\u53c2\u6570\u4e2a\u6570<br>  if(arguments.length !== 3){<br>    throw new Error(\"\u8be5\u51fd\u6570\u53ea\u652f\u6301\u4e09\u4e2a\u53c2\u6570 sel , data , c\")<br>  }<br>  \/\/ \u68c0\u67e5\u53c2\u6570C\u7684\u7c7b\u578b<br>  if(&#91;\"string\",\"number\"].includes(typeof c)){<br>    \/\/ \u8bf4\u660e\u73b0\u5728\u73b0\u5728\u8c03\u7528h\u51fd\u6570\u662f\u5f62\u60011<br>    return vnode(sel,data,undefined,c,undefined)<br>  } else if(Array.isArray(c)){<br>    \/\/ \u8bf4\u660e\u51fa\u73b0\u7684\u662f\u5f62\u60012<br>    let children = &#91;]<br>    \/\/ \u904d\u5386\u6570\u7ec4,\u6536\u96c6\u6570\u7ec4\u7684\u8282\u70b9<br>    c.forEach((item,index)=&gt;{<br>      if(typeof item === \"object\" &amp;&amp; item.hasOwnProperty('sel')){<br>        children&#91;index] = item<br>      }else{<br>        throw new Error(\"\u4f20\u5165\u7684\u6570\u7ec4\u53c2\u6570\u4e0d\u662fH\u51fd\u6570\")<br>      }<br>    })<br>    return vnode(sel,data,children,undefined,undefined)<br>  } else if(typeof c === \"object\" &amp;&amp; c.hasOwnProperty('sel')){<br>    let children = &#91;c]<br>    return vnode(sel,data,children,undefined,undefined)<br>  }<br>}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u865a\u62dfDOM \u7528JS\u5bf9\u8c61\u63cf\u8ff0DOM\u7684\u5c42\u6b21\u7ed3\u6784\uff0cDOM\u4e2d\u7684\u4e00\u5207\u5c5e\u6027\u90fd\u5728\u865a\u62dfDOM\u4e2d\u6709\u5bf9\u5e94\u7684\u5c5e\u6027 \u4f18\u70b9 \u53ef\u4ee5\u51cf\u5c11DO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-274","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/274","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}