Maison > interface Web > js tutoriel > le corps du texte

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

PHP中文网
Libérer: 2017-07-12 18:10:35
original
1190 Les gens l'ont consulté
<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>
Copier après la connexion

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!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
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
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal