Is this a valid/safe way to clone any object/function/etc. in ES6?
I'm wondering whether this is a safe approach to cloning a variable that could be anything:
// the original variable definition:
let varIWantToCopy = 'hello world'
// the clone variable's definition:
let {varIWantToCopy: cloneOfVarIWantToCopy} = {varIWantToCopy}
// mutating the original value:
varIWantToCopy = 'goodbye world'
// should log out 'hello world':
console.log(cloneOfVarIWantToCopy)
Thank you for taking the time to look this over.
Kind regards,
Harry
javascript variables ecmascript-6 clone immutability
add a comment |
I'm wondering whether this is a safe approach to cloning a variable that could be anything:
// the original variable definition:
let varIWantToCopy = 'hello world'
// the clone variable's definition:
let {varIWantToCopy: cloneOfVarIWantToCopy} = {varIWantToCopy}
// mutating the original value:
varIWantToCopy = 'goodbye world'
// should log out 'hello world':
console.log(cloneOfVarIWantToCopy)
Thank you for taking the time to look this over.
Kind regards,
Harry
javascript variables ecmascript-6 clone immutability
2
No, because no clone or copy is made by that code. There is no native facility for a general-case "clone" or "copy" from an object because what that means is subjective.
– Pointy
Nov 14 '18 at 1:17
strings are immutable. You can just dolet clone = varIWantToCopy
and then you can modify clone without affectingvarIWantToCopy
– Doug Coburn
Nov 14 '18 at 1:19
add a comment |
I'm wondering whether this is a safe approach to cloning a variable that could be anything:
// the original variable definition:
let varIWantToCopy = 'hello world'
// the clone variable's definition:
let {varIWantToCopy: cloneOfVarIWantToCopy} = {varIWantToCopy}
// mutating the original value:
varIWantToCopy = 'goodbye world'
// should log out 'hello world':
console.log(cloneOfVarIWantToCopy)
Thank you for taking the time to look this over.
Kind regards,
Harry
javascript variables ecmascript-6 clone immutability
I'm wondering whether this is a safe approach to cloning a variable that could be anything:
// the original variable definition:
let varIWantToCopy = 'hello world'
// the clone variable's definition:
let {varIWantToCopy: cloneOfVarIWantToCopy} = {varIWantToCopy}
// mutating the original value:
varIWantToCopy = 'goodbye world'
// should log out 'hello world':
console.log(cloneOfVarIWantToCopy)
Thank you for taking the time to look this over.
Kind regards,
Harry
javascript variables ecmascript-6 clone immutability
javascript variables ecmascript-6 clone immutability
asked Nov 14 '18 at 1:16
Harry Solovay
156
156
2
No, because no clone or copy is made by that code. There is no native facility for a general-case "clone" or "copy" from an object because what that means is subjective.
– Pointy
Nov 14 '18 at 1:17
strings are immutable. You can just dolet clone = varIWantToCopy
and then you can modify clone without affectingvarIWantToCopy
– Doug Coburn
Nov 14 '18 at 1:19
add a comment |
2
No, because no clone or copy is made by that code. There is no native facility for a general-case "clone" or "copy" from an object because what that means is subjective.
– Pointy
Nov 14 '18 at 1:17
strings are immutable. You can just dolet clone = varIWantToCopy
and then you can modify clone without affectingvarIWantToCopy
– Doug Coburn
Nov 14 '18 at 1:19
2
2
No, because no clone or copy is made by that code. There is no native facility for a general-case "clone" or "copy" from an object because what that means is subjective.
– Pointy
Nov 14 '18 at 1:17
No, because no clone or copy is made by that code. There is no native facility for a general-case "clone" or "copy" from an object because what that means is subjective.
– Pointy
Nov 14 '18 at 1:17
strings are immutable. You can just do
let clone = varIWantToCopy
and then you can modify clone without affecting varIWantToCopy
– Doug Coburn
Nov 14 '18 at 1:19
strings are immutable. You can just do
let clone = varIWantToCopy
and then you can modify clone without affecting varIWantToCopy
– Doug Coburn
Nov 14 '18 at 1:19
add a comment |
1 Answer
1
active
oldest
votes
I think you should use lodash if you really want to clone an object.
There are several clone functions in lodash that you can use depending on your requirements.
// or you can use lodash/clone
import cloneDeep from 'lodash/cloneDeep';
var objects = [{ 'a': 1 }, { 'b': 2 }];
var cloned = cloneDeep(objects);
console.log(cloned[0] === objects[0]); // this will be false
So if you change the value of cloned[0], the value of object[0] won't change.
Thank you @xeiton! Would this work if a function was assigned toobjects
?
– Harry Solovay
Nov 14 '18 at 13:01
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291810%2fis-this-a-valid-safe-way-to-clone-any-object-function-etc-in-es6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you should use lodash if you really want to clone an object.
There are several clone functions in lodash that you can use depending on your requirements.
// or you can use lodash/clone
import cloneDeep from 'lodash/cloneDeep';
var objects = [{ 'a': 1 }, { 'b': 2 }];
var cloned = cloneDeep(objects);
console.log(cloned[0] === objects[0]); // this will be false
So if you change the value of cloned[0], the value of object[0] won't change.
Thank you @xeiton! Would this work if a function was assigned toobjects
?
– Harry Solovay
Nov 14 '18 at 13:01
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
add a comment |
I think you should use lodash if you really want to clone an object.
There are several clone functions in lodash that you can use depending on your requirements.
// or you can use lodash/clone
import cloneDeep from 'lodash/cloneDeep';
var objects = [{ 'a': 1 }, { 'b': 2 }];
var cloned = cloneDeep(objects);
console.log(cloned[0] === objects[0]); // this will be false
So if you change the value of cloned[0], the value of object[0] won't change.
Thank you @xeiton! Would this work if a function was assigned toobjects
?
– Harry Solovay
Nov 14 '18 at 13:01
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
add a comment |
I think you should use lodash if you really want to clone an object.
There are several clone functions in lodash that you can use depending on your requirements.
// or you can use lodash/clone
import cloneDeep from 'lodash/cloneDeep';
var objects = [{ 'a': 1 }, { 'b': 2 }];
var cloned = cloneDeep(objects);
console.log(cloned[0] === objects[0]); // this will be false
So if you change the value of cloned[0], the value of object[0] won't change.
I think you should use lodash if you really want to clone an object.
There are several clone functions in lodash that you can use depending on your requirements.
// or you can use lodash/clone
import cloneDeep from 'lodash/cloneDeep';
var objects = [{ 'a': 1 }, { 'b': 2 }];
var cloned = cloneDeep(objects);
console.log(cloned[0] === objects[0]); // this will be false
So if you change the value of cloned[0], the value of object[0] won't change.
edited Nov 16 '18 at 12:54
answered Nov 14 '18 at 2:01
xeiton
2216
2216
Thank you @xeiton! Would this work if a function was assigned toobjects
?
– Harry Solovay
Nov 14 '18 at 13:01
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
add a comment |
Thank you @xeiton! Would this work if a function was assigned toobjects
?
– Harry Solovay
Nov 14 '18 at 13:01
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
Thank you @xeiton! Would this work if a function was assigned to
objects
?– Harry Solovay
Nov 14 '18 at 13:01
Thank you @xeiton! Would this work if a function was assigned to
objects
?– Harry Solovay
Nov 14 '18 at 13:01
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
@HarrySolovay, I am not 100% sure, but my guess is that it should work with functions too, since functions are a type of object in JS. If this answer helped you please accept as an answer or up-vote, or both :P ... Thank you!
– xeiton
Nov 14 '18 at 22:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291810%2fis-this-a-valid-safe-way-to-clone-any-object-function-etc-in-es6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
No, because no clone or copy is made by that code. There is no native facility for a general-case "clone" or "copy" from an object because what that means is subjective.
– Pointy
Nov 14 '18 at 1:17
strings are immutable. You can just do
let clone = varIWantToCopy
and then you can modify clone without affectingvarIWantToCopy
– Doug Coburn
Nov 14 '18 at 1:19