Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add how to clone section #67

Closed
Kikobeats opened this issue Jan 23, 2018 · 0 comments · Fixed by #77
Closed

Add how to clone section #67

Kikobeats opened this issue Jan 23, 2018 · 0 comments · Fixed by #77

Comments

@Kikobeats
Copy link
Owner

Kikobeats commented Jan 23, 2018

This is a popular internet topic.

About:

  1. How to clone an array
const newArray = arr.slice(0)
const anotherArray = [...arr]
  1. How to clone a plain object
const newObj = Object.assign({}, obj)
const anotherObj = {...obj}

also known as shallow clone:

> third = {hello: 'world'}
> obj = {foo: 'bar', third}
{ foo: 'bar', third: { hello: 'world' } }
> shallow = Object.assign({}, obj)
{ foo: 'bar', third: { hello: 'world' } }
> shallow
{ foo: 'bar', third: { hello: 'world' } }
> third
{ hello: 'world' }
> third.hello = 'test'
'test'
> shallow
{ foo: 'bar', third: { hello: 'test' } }
  1. How to clone a deep object and why a simple clone is not enough (talk about passing by value and reference).
const deepClone = obj => {
  let clone = Object.assign({}, obj);
  Object.keys(clone).forEach(
    key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
  );
  return clone;
};
  1. Why JSON.parse(JSON.stringify(value)) is not enough.
  • Will throw error in circular objects.
  • The JSON.stringify conversion has to be lossless. Therefore, methods are not allowed as members of type function are ignored by the JSON stringifier. The undefined value is not allowed either. Object keys with undefined value are omitted, while undefined values in arrays are substituted by null.
  • Members of the Date object become ISO-8601 strings, not representing the timezone of the client. The value new Date(2011,0,1) becomes "2010-12-31T23:00:00.000Z" after executing the above deep copy method in case you live in Central Europe.
  1. Why not possible do it async style.

parse/stringify is a CPU intensive task, so no I/O is here and not supported from low level Node.js API.
The only "async" way possible is in deepClone wrap each deepClone call into a process.nextTick, simulating deferrer it.

Inspiration:

@Kikobeats Kikobeats changed the title Add clone objects section Add how to clone section Jan 24, 2018
Kikobeats added a commit that referenced this issue May 7, 2018
Kikobeats added a commit that referenced this issue May 7, 2018
Kikobeats added a commit that referenced this issue May 7, 2018
Kikobeats added a commit that referenced this issue May 8, 2018
Kikobeats added a commit that referenced this issue May 8, 2018
Kikobeats added a commit that referenced this issue May 8, 2018
* Add how to clone
close #67

* Tweaks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant