JS - 数据类型的值拷贝函数(深拷贝)

PHP中文网
Lepaskan: 2017-07-12 18:10:35
asal
1195 orang telah melayarinya
<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>
Salin selepas log masuk

Atas ialah kandungan terperinci JS - 数据类型的值拷贝函数(深拷贝). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan