Heim > Web-Frontend > js-Tutorial > JS – Wertkopierfunktion des Datentyps (tiefe Kopie)

JS – Wertkopierfunktion des Datentyps (tiefe Kopie)

PHP中文网
Freigeben: 2017-07-12 18:10:35
Original
1214 Leute haben es durchsucht
<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>
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonJS – Wertkopierfunktion des Datentyps (tiefe Kopie). Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage