Also have some issues when clone object with JSON.parse(JSON.stringify)
Can you check?
classPerson{#name="";constructor(name){this.#name=name;}greet(){console.log("hi, "+this.#name);}}constp=newPerson("Peter");constobj={d:newDate(),p};constnewObj=JSON.parse(JSON.stringify(obj));console.log(typeofobj.d);// output: objectconsole.log(typeofnewObj.d);// output: stringobj.p.greet();// output: hi, PeternewObj.p.greet();// Uncaught TypeError: newObj.p.greet is not a function
Thank you,
And I have some questions about this,
When I copy:
constobj={n1:3,n2:newNumber(4)};constnewObj=JSON.parse(JSON.stringify(obj));console.log(typeofobj.n1);// numberconsole.log(typeofobj.n2);// objectconsole.log(typeofnewObj.n1);// numberconsole.log(typeofnewObj.n2);// number
Also have some issues when clone object with
JSON.parse(JSON.stringify)
Can you check?
I should of mentioned...the JSON method won’t be able to clone methods 😣
Check out this article, it has more info:
google.ca/amp/s/scotch.io/bar-talk...
Thank you,
And I have some questions about this,
When I copy:
obj.n2
andnewObj.n2
are the same?And as in my previous comment, when I copy:
Another question, when I create an object by
Object.create
like this:Why that, we cannot clone it without readable it? Please help me understand, thank you!