gsLiu
如何赋值一个对象a到另一个变量b,另一个变量b发生改变原对象a保持不变。
 
参考资料:
  1. http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-an-object/5344074#534407
  2. http://web.archive.org/web/20140328224025/http://jsperf.com/cloning-an-object/2
  3. http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object
  4. http://stackoverflow.com/questions/18359093/how-to-copy-javascript-object-to-new-variable-not-by-reference
//几种方式:
//for  in
     for(var attr in oldObj){
     var newOld={};
     if(oldObj.hasOwlProprity(attr)){
     newOld[attr]=oldObj[attr];
     }
}
//JSON
     var newObj=JSON.paser(JSON.stringify(oldObj));
//jQuery
浅拷贝
var newObj=jQuery.extend({},old);
深拷贝
var newObj=jQuery.extend(true,{},old);

 

分类:

技术点:

相关文章:

  • 2021-06-07
  • 2022-02-21
  • 2022-02-19
  • 2021-11-21
  • 2021-12-27
  • 2022-01-27
  • 2022-12-23
  • 2021-06-27
猜你喜欢
  • 2022-01-09
  • 2022-01-05
  • 2021-08-13
  • 2022-02-06
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案