Home > Article > Web Front-end > JS - Data type value copy function (deep copy)
<code class="hljs"><span class="hljs-function"><span class="hljs-keyword">function motto<span class="hljs-title">Clone (<span class="hljs-params">obj) { <span class="hljs-keyword">if (obj === <span class="hljs-literal">null || <span class="hljs-keyword">typeof obj !== <span class="hljs-string">'object') <span class="hljs-keyword">return obj; <span class="hljs-keyword">if (obj <span class="hljs-keyword">instanceof <span class="hljs-built_in">Boolean) <span class="hljs-keyword">return <span class="hljs-keyword">new <span class="hljs-built_in">Boolean(obj.valueOf()); <span class="hljs-keyword">if (obj <span class="hljs-keyword">instanceof <span class="hljs-built_in">Number) <span class="hljs-keyword">return <span class="hljs-keyword">new <span class="hljs-built_in">Number(obj.valueOf()); <span class="hljs-keyword">if (obj <span class="hljs-keyword">instanceof <span class="hljs-built_in">String) <span class="hljs-keyword">return <span class="hljs-keyword">new <span class="hljs-built_in">String(obj.valueOf()); <span class="hljs-keyword">if (obj <span class="hljs-keyword">instanceof <span class="hljs-built_in">RegExp) <span class="hljs-keyword">return <span class="hljs-keyword">new <span class="hljs-built_in">RegExp(obj.valueOf()); <span class="hljs-keyword">if (obj <span class="hljs-keyword">instanceof <span class="hljs-built_in">Date) <span class="hljs-keyword">return <span class="hljs-keyword">new <span class="hljs-built_in">Date(obj.valueOf()); <span class="hljs-keyword">var cpObj = obj <span class="hljs-keyword">instanceof <span class="hljs-built_in">Array ? [] : {}; <span class="hljs-keyword">for (<span class="hljs-keyword">var key <span class="hljs-keyword">in obj) cpObj[key] = myClone(obj[key]); <span class="hljs-keyword">return cpObj; }<br><br>支持的数据类型或格式有:Boolean,Number,String,RegExp,Date,Function,Array,JSON<br>支持深拷贝(循环迭代),如:<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code><code class="hljs"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-title"><span class="hljs-params"><span class="hljs-keyword"><span class="hljs-literal"><span class="hljs-keyword"><span class="hljs-string"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword">var obj = {<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code><code class="hljs"><span class="hljs-function"><span class="hljs-keyword"><span class="hljs-title"><span class="hljs-params"><span class="hljs-keyword"><span class="hljs-literal"><span class="hljs-keyword"><span class="hljs-string"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-built_in"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword"><span class="hljs-keyword"> id: 1,<br> name: 'xxx',<br> sayName: function () {<br> console.log('my name is' + this.name);<br> },<br> childs: [<br> {}, <br> {}, <br> ...<br> ],<br> opts: {<br> xxx: [],<br> ...<br> },<br> ...<br>};<br>var newObj = mottoClone(obj);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
The above is the detailed content of JS - Data type value copy function (deep copy). For more information, please follow other related articles on the PHP Chinese website!