Giter VIP home page Giter VIP logo

Comments (14)

honpery avatar honpery commented on September 27, 2024 5

@xwHoward 所提到的深拷贝方法会忽略undefined和function,所以需要采用文章的方法再次封装 - -

from cc.

jjeejj avatar jjeejj commented on September 27, 2024 4

推荐看我的这篇文章:JSON

from cc.

xwHoward avatar xwHoward commented on September 27, 2024

使用JSON.stringify()和JSON.parse()做对象的深拷贝:

var data = {
  a: 'abc',
  c: {
    x: {
      foo: 'xxx',
      bar: 'yyy'
    },
    y: 'yyy'
  }
};

var copy = JSON.stringify(data);
var copyObj = JSON.parse(copy);

from cc.

bouquetrender avatar bouquetrender commented on September 27, 2024

@xwHoward 这种深拷贝方式有什么副作用吗

from cc.

keizure avatar keizure commented on September 27, 2024

直接用扩展运算符

from cc.

lulusir avatar lulusir commented on September 27, 2024

@sakuyakun 副作用就是文中提到的undefined和function会忽略咯,还有就是不支持JSON的浏览器就用不了

from cc.

refanbanzhang avatar refanbanzhang commented on September 27, 2024

比较有意思

from cc.

xxyj avatar xxyj commented on September 27, 2024

@lulusir 还有如果字符串中含有'\n',JSON.parse解析是会报错的

from cc.

xwHoward avatar xwHoward commented on September 27, 2024

@honpery @sakuyakun @lulusir @xxyj 确实这种方法有一定局限性,但在对数据结构明了、性能要求比较高(相比用递归进行对象复制)的时候可以用这种方式快速进行对象深拷贝,算是一种trick啦

from cc.

yunchangyue avatar yunchangyue commented on September 27, 2024

很好

from cc.

qianlongo avatar qianlongo commented on September 27, 2024

赞 可以的

from cc.

zhengsk avatar zhengsk commented on September 27, 2024

@xwHoward 按我的了解,该方法复制对象的性能比直接使用传统方式复制对象更慢。
https://jsperf.com/deep-copy-vs-json-stringify-json-parse/10

确实这种方法有一定局限性,但在对数据结构明了、性能要求比较高(相比用递归进行对象复制)的时候可以用这种方式快速进行对象深拷贝,算是一种trick啦

from cc.

chinghanho avatar chinghanho commented on September 27, 2024

@lulusir 瀏覽器支援不是問題了。

screen shot 2017-07-31 at 10 16 22 am

from cc.

yingyuk avatar yingyuk commented on September 27, 2024
var myMap = new Map();
myMap.set(0, 'zero');

var mySet = new Set();
mySet.add(1);

class test {
  constructor(opt) {
    this.opt = opt;
  }
}

JSON.stringify({
  String: 'string',
  Boolean: true,
  Number: 123,
  NaN: NaN, // null,
  Infinity: Infinity, // null
  null: null, // null
  undefined: undefined, // 没有 key
  Array: [1, 2, 3],
  Object: {
    foo: 'bar',
  },
  Symbol: Symbol('foo'), // {}
  Map: myMap, // {}
  Set: mySet, // {}
  Promise: new Promise(function(resolve, reject) {}), // {}
  Proxy: new Proxy({}, {}), // {}
  Class: test, //  没有 key
  ClassA: new test(123), // { "opt": 123 },
  Math: Math, // {}
  // Buffer: new Buffer('123'), // nodeJs  '{"Buffer":{"type":"Buffer","data":[49,50,51]}}'
  Error: new Error('error'), // {}
  Function: function() { // 没有 key
    console.info('hi');
  },
  Date: new Date(), // "2017-08-01T13:52:48.628Z",
  RegExp: /RegExp/, // {}
}, null, 2);

/*
"{
  "String": "string",
  "Boolean": true,
  "Number": 123,
  "NaN": null,
  "Infinity": null,
  "null": null,
  "Array": [
    1,
    2,
    3
  ],
  "Object": {
    "foo": "bar"
  },
  "Map": {},
  "Set": {},
  "Promise": {},
  "Proxy": {},
  "ClassA": {
    "opt": 123
  },
  "Math": {},
  "Error": {},
  "Date": "2017-08-01T14:04:58.522Z",
  "RegExp": {}
}"
 */

from cc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.