Maison >interface Web >js tutoriel >JS - Fonction de copie de valeur du type de données (copie profonde)

JS - Fonction de copie de valeur du type de données (copie profonde)

PHP中文网
PHP中文网original
2017-07-12 18:10:351210parcourir
<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>

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn