{"id":3319,"date":"2022-04-23T19:51:10","date_gmt":"2022-04-23T11:51:10","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=3319"},"modified":"2022-04-23T19:51:10","modified_gmt":"2022-04-23T11:51:10","slug":"%e6%b7%b1%e6%8b%b7%e8%b4%9d%e5%92%8c%e6%b5%85%e6%8b%b7%e8%b4%9d","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=3319","title":{"rendered":"\u6df1\u62f7\u8d1d\u548c\u6d45\u62f7\u8d1d"},"content":{"rendered":"<p>\u6df1\u62f7\u8d1d\u548c\u6d45\u62f7\u8d1d\u662f\u7ecf\u5e38\u5728\u9762\u8bd5\u4e2d\u4f1a\u51fa\u73b0\u7684\uff0c\u4e3b\u8981\u8003\u5bdf\u4f60\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b\u7684\u7406\u89e3\u6df1\u5ea6\u3002<\/p>\n<h3>\u4e00\u3001\u5148\u4e86\u89e3\u5185\u5b58<\/h3>\n<p>1. \u5185\u5b58\u7b80\u5355\u5212\u5206\u4e3a\u5806\u533a\u57df\u548c\u6808\u533a\u57df<br \/>\n2. \u590d\u6742\u6570\u636e\u7c7b\u578b\u6570\u636e\u5b58\u50a8\u5728\u5806\u533a\u57df\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53d8\u91cf\u548c\u5f15\u7528\u53d8\u91cf\u5b58\u50a8\u5728\u6808\u533a\u57df<\/p>\n<h3>\u4e8c\u3001\u62f7\u8d1d- \u590d\u5236obj\u5bf9\u8c61\u5f97\u5230\u4e00\u4e2a\u5168\u65b0\u7684\u5bf9\u8c61\uff0c<\/h3>\n<p>1. \u6d45\u62f7\u8d1d &#8211; \u590d\u5236\u7684obj\u5bf9\u8c61\u53ea\u590d\u5236\u4e00\u5c42\uff0c\u5982\u679c\u5bf9\u8c61\u5c5e\u6027\u503c\u662f\u5bf9\u8c61\u5219\u4e0d\u80fd\u590d\u5236<br \/>\n2. \u6df1\u62f7\u8d1d &#8211; \u5b8c\u5168\u590d\u5236\u7684obj\u5bf9\u8c61\uff0c\u5982\u679c\u5bf9\u8c61\u5c5e\u6027\u503c\u662f\u5bf9\u8c61\u4e00\u8d77\u590d\u5236\u5f97\u5230\u5168\u65b0\u5bf9\u8c61<br \/>\n3. \u793a\u4f8b<\/p>\n<p>[java]const obj = {<br \/>\n\tname: &#8216;jack&#8217;,<br \/>\n\tage: 18,<br \/>\n\tfun: { swiming: &#8216;\u6e38\u6cf3&#8217; }, \/\/ Object<br \/>\n    }<br \/>\n[\/java]<\/p>\n<h5>\u6d45\u62f7\u8d1d<\/h5>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/04\/20220423193220.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3320\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/04\/20220423193220.png\" alt=\"20220423193220\" width=\"956\" height=\"472\" \/><\/a><\/p>\n<h5>\u6df1\u62f7\u8d1d<\/h5>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/04\/20220423193605.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3324\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2022\/04\/20220423193605.png\" alt=\"20220423193605\" width=\"1373\" height=\"653\" \/><\/a><\/p>\n<h3>\u4e09\u3001\u5b9e\u73b0\u65b9\u5f0f<\/h3>\n<h5>1. JSON.parse(JSON.stringify(obj))<\/h5>\n<p>\u7f3a\u70b9: \u6570\u636e\u7c7b\u578b\u662fFunction\u6216\u6570\u636e\u503c\u4e3aundefined\u65e0\u6cd5\u62f7\u8d1d<br \/>\n[java]<br \/>\nconst obj = {<br \/>\n                name: &#8216;jack&#8217;,<br \/>\n                age: 18,<br \/>\n                hobby: {swiming:&#8217;\u6e38\u6cf3&#8217;},<br \/>\n                arr: [{ score: 98 }],<br \/>\n                say: () =&gt; {},         \/\/ Function    \u65e0\u6cd5\u62f7\u8d1d<br \/>\n                number: undefined,     \/\/ undefined   \u65e0\u6cd5\u62f7\u8d1d<br \/>\n            }[\/java]<\/p>\n<h5>2. Object.assign(obj)\u6216\u5c55\u5f00\u8fd0\u7b97\u7b26{&#8230;obj}<\/h5>\n<p>\u7f3a\u70b9\uff1a\u53ea\u80fd\u62f7\u8d1d\u4e00\u5c42,\u5982\u679c\u5c5e\u6027\u503c\u662f\u5bf9\u8c61\uff0c\u65e0\u6cd5\u62f7\u8d1d<br \/>\n[java]<br \/>\nconst obj = {<br \/>\n                name: &#8216;jack&#8217;,<br \/>\n                age: 18,<br \/>\n                hobby: {swiming:&#8217;\u6e38\u6cf3&#8217;}, \/\/ Object   \u65e0\u6cd5\u62f7\u8d1d<br \/>\n                arr: [{ score: 98 }],   \/\/ Array     \u65e0\u6cd5\u62f7\u8d1d<br \/>\n                say: () =&gt; {},         \/\/ Function   \u65e0\u6cd5\u62f7\u8d1d<br \/>\n                number: undefined,     \/\/ undefined<br \/>\n            }[\/java]<\/p>\n<h5>3. \u9012\u5f52 cloneDeep()<\/h5>\n<h5>4. \u4f18\u5316:<\/h5>\n<blockquote>\n<ul>\n<li>1. \u5982\u679c\u5bf9\u8c61\u5c5e\u6027\u503c\u6ca1\u6709\u5bf9\u8c61\uff0c\u53ea\u6709\u4e00\u5c42,\u4f7f\u7528\u5c55\u5f00\u8fd0\u7b97\u7b26{&#8230;obj}<\/li>\n<li>2. \u5982\u679c\u5bf9\u8c61\u6570\u636e\u7c7b\u578b\u4e0d\u662fFunction\u4e5f\u4e0d\u662f\u6570\u636e\u503c\u4e3aundefined,\u4f7f\u7528JSON.parse(JSON.stringify(obj))<\/li>\n<li>3. \u5426\u5219\u4f7f\u7528\u9012\u5f52<\/li>\n<\/ul>\n<\/blockquote>\n<h3>\u56db\u3001\u6848\u4f8b<\/h3>\n<p>[java]\/**<br \/>\n * \u4f7f\u7528 JSON.parse(JSON.stringify(obj)) \u62f7\u8d1d<br \/>\n *\/<br \/>\nconst testJSON = () =&gt; {<br \/>\n\tconst obj = {<br \/>\n\t\tname: &#8216;jack&#8217;,<br \/>\n\t\tage: 18,<br \/>\n\t\thobby: { swiming: &#8216;\u6e38\u6cf3&#8217; }, \/\/ Object<br \/>\n\t\tarr: [{ score: 98 }], \/\/ Array<br \/>\n\t\tsay: () =&gt; {}, \/\/Function<br \/>\n\t\tnumber: undefined, \/\/undefined<br \/>\n\t}<\/p>\n<p>\tconst cloneObj = JSON.parse(JSON.stringify(obj))<br \/>\n\tconsole.log(&#8216;obj :&#8217;, obj, &#8216;\\n cloneObj :&#8217;, cloneObj)<br \/>\n}<br \/>\n[\/java]<br \/>\n[java]\/**<br \/>\n * \u4f7f\u7528 1. Object.assign(obj)<br \/>\n *      2. \u5c55\u5f00\u8fd0\u7b97\u7b26{&#8230;obj}<br \/>\n *\/<br \/>\nconst testAssign = () =&gt; {<br \/>\n\tconst obj = {<br \/>\n\t\tname: &#8216;jack&#8217;,<br \/>\n\t\tage: 18,<br \/>\n\t\thobby: { swiming: &#8216;\u6e38\u6cf3&#8217; }, \/\/ Object<br \/>\n\t\tarr: [{ score: 98 }], \/\/ Array<br \/>\n\t\tsay: () =&gt; {console.log(&#8216;say&gt;&gt;&#8217;)}, \/\/Function<br \/>\n\t\tnumber: undefined, \/\/undefined<br \/>\n\t}<\/p>\n<p>\t\/\/ const cloneObj = Object.assign(obj)<br \/>\n\tconst cloneObj = { &#8230;obj }<br \/>\n\tcloneObj.name = &#8216;rose&#8217;<br \/>\n\tcloneObj.age = 20<br \/>\n\tcloneObj.hobby.swiming = &#8216;\u4e0d\u4f1a\u6e38\u6cf3&#8217; \/\/ \u65e0\u6cd5\u62f7\u8d1d,cloneObj.hobby == obj.hobby<br \/>\n    cloneObj.arr[0].score = 100<br \/>\n\tcloneObj.say()<br \/>\n\tconsole.log(&#8216;obj :&#8217;, obj, &#8216;\\n cloneObj :&#8217;, cloneObj)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u6df1\u62f7\u8c9d-\u9012\u5f52\u5b9e\u73b0<br \/>\n * @param {*} data<br \/>\n * @returns<br \/>\n *\/<br \/>\nconst cloneDeep = data =&gt; {<br \/>\n\tconst newData = Array.isArray(data) ? [] : {}<br \/>\n\tfor (let key in data) {<br \/>\n\t\tif (data[key] &amp;&amp; typeof data[key] === &#8216;object&#8217;) {<br \/>\n\t\t\tnewData[key] = cloneDeep(data[key])<br \/>\n\t\t} else {<br \/>\n\t\t\tnewData[key] = data[key]<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn newData<br \/>\n}[\/java]<br \/>\n[java]\/**<br \/>\n * test\u9012\u5f52-\u62f7\u8d1d<br \/>\n *\/<br \/>\nconst testCloneDeep = () =&gt; {<br \/>\n\tconst obj = {<br \/>\n\t\tname: &#8216;jack&#8217;,<br \/>\n\t\tage: 18,<br \/>\n\t\thobby: { swiming: &#8216;\u6e38\u6cf3&#8217; }, \/\/ Object<br \/>\n\t\tarr: [{ score: 98 }],<br \/>\n\t\tsay: () =&gt; {}, \/\/Function<br \/>\n\t\tnumber: undefined, \/\/undefined<br \/>\n\t\tstudents: [<br \/>\n\t\t\t{<br \/>\n\t\t\t\tstudent: {<br \/>\n\t\t\t\t\tnum: {<br \/>\n\t\t\t\t\t\tid: 1,<br \/>\n\t\t\t\t\t},<br \/>\n\t\t\t\t},<br \/>\n\t\t\t},<br \/>\n\t\t],<br \/>\n\t}<br \/>\n\tconst cloneObj = cloneDeep(obj)<br \/>\n\tcloneObj.name = &#8216;rose&#8217;<br \/>\n\tcloneObj.age = 20<br \/>\n\tcloneObj.hobby.swiming = &#8216;\u4e0d\u4f1a\u6e38\u6cf3&#8217;<br \/>\n\tcloneObj.arr[0].score = 100<br \/>\n\tcloneObj.students[0].student.num.id = 100<br \/>\n\tconsole.log(&#8216;obj :&#8217;, obj, &#8216;\\n cloneObj :&#8217;, cloneObj)<br \/>\n\tconsole.log(cloneObj.students[0].student.num.id === obj.students[0].student.num.id)<br \/>\n}[\/java]<\/p>\n<p>[java]\/**<br \/>\n * \u5224\u65ad\u5bf9\u8c61\u5c5e\u6027\u503c\u662f\u5426\u6709\u5bf9\u8c61<br \/>\n * @param {} data<br \/>\n * @returns<br \/>\n *\/<br \/>\nconst isObjectValue = data =&gt; {<br \/>\n\tfor (let key in data) {<br \/>\n\t\tif (data[key] &amp;&amp; typeof data[key] === &#8216;object&#8217;) {<br \/>\n\t\t\tif(Object.prototype.toString.call(data[key]) !== &#8216;[object Function]&#8217;){<br \/>\n                return true<br \/>\n            }<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<br \/>\n[java]\/**<br \/>\n * \u9012\u5f52\u5224\u65ad\u6570\u636e\u7c7b\u578b<br \/>\n *   Function\u6216undefined\u8fd4\u56de\u4e3atrue<br \/>\n *\/<br \/>\nconst isFunctionOrUndefined = data =&gt; {<br \/>\n\tfor (let key in data) {<br \/>\n\t\tif (data[key] === undefined) {<br \/>\n\t\t\treturn true<br \/>\n\t\t} else if (data[key] &amp;&amp; Object.prototype.toString.call(data[key]) === &#8216;[object Function]&#8217;) {<br \/>\n\t\t\treturn true \/\/Function<br \/>\n\t\t} else if (data[key] &amp;&amp; typeof data[key] === &#8216;object&#8217;) {<br \/>\n\t\t\tisFunctionOrUndefined(data[key])<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<br \/>\n[java]\/**<br \/>\n * \u62f7\u8d1d\u5bf9\u8c61 \u4f18\u5316\u65b9\u6848<br \/>\n * @param {*} obj \u539f\u5bf9\u8c61<br \/>\n * @param {*} cloneOjb \u8fd4\u56de\u62f7\u8d1d\u5bf9\u8c61<br \/>\n *\/<br \/>\nconst cloneDeepObj = obj =&gt; {<br \/>\n\tif (obj === undefined) {<br \/>\n\t\tthrow new TypeError(&#8216;param is not undefined&#8217;)<br \/>\n\t}<br \/>\n\t\/\/\u5224\u65ad\u62f7\u8d1d\u5bf9\u8c61\u53ea\u6709\u4e00\u5c42\u53ca\u5c5e\u6027\u503c\u90fd\u4e0d\u662f\u5bf9\u8c61,\u4f7f\u7528Object.assign()<br \/>\n\tif (!isObjectValue(obj)) {<br \/>\n\t\treturn  { &#8230;obj }<br \/>\n\t}<br \/>\n\t\/\/\u5224\u65ad\u7c7b\u578b,\u5982\u679c\u4e0d\u662fFunction\u6216undefined\u4f7f\u7528JSON\u65b9\u5f0f<br \/>\n\tif (!isFunctionOrUndefined(obj)) {<br \/>\n\t\treturn JSON.parse(JSON.stringify(obj))<br \/>\n\t}<\/p>\n<p>    return cloneDeep(obj)<\/p>\n<p>}<br \/>\n[\/java]<br \/>\n[java]const testCloneDeepObj = () =&gt; {<br \/>\n\tconst obj = {<br \/>\n\t\tname: &#8216;jack&#8217;,<br \/>\n\t\tage: 18,<br \/>\n\t\thobby: { swiming: &#8216;\u6e38\u6cf3&#8217; },<br \/>\n\t\tarr: [{ score: 98 }],<br \/>\n\t\tsay: function(){console.log(this.name)},<br \/>\n\t\tnumber: undefined,<br \/>\n\t}<br \/>\n\tconst cloneObj = cloneDeepObj(obj)<br \/>\n\tcloneObj.name = &#8216;rose&#8217;<br \/>\n\tcloneObj.age = 20<br \/>\n\tcloneObj.hobby.swiming = &#8216;\u4e0d\u4f1a\u6e38\u6cf3&#8217;<br \/>\n\tcloneObj.arr[0].score = 100<br \/>\n\tconsole.log(&#8216;obj :&#8217;, obj, &#8216;\\n cloneObj :&#8217;, cloneObj)<br \/>\n    cloneObj.say()<br \/>\n    obj.say()<br \/>\n}<\/p>\n<p>testCloneDeepObj()[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u62f7\u8d1d\u548c\u6d45\u62f7\u8d1d\u662f\u7ecf\u5e38\u5728\u9762\u8bd5\u4e2d\u4f1a\u51fa\u73b0\u7684\uff0c\u4e3b\u8981\u8003\u5bdf\u4f60\u5bf9\u57fa\u672c\u7c7b\u578b\u548c\u5f15\u7528\u7c7b\u578b\u7684\u7406\u89e3\u6df1\u5ea6\u3002 \u4e00\u3001\u5148\u4e86\u89e3\u5185\u5b58 1. \u5185\u5b58\u7b80 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[109,105],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3319"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3319"}],"version-history":[{"count":15,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3319\/revisions"}],"predecessor-version":[{"id":3337,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/3319\/revisions\/3337"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}