Is this a valid/safe way to clone any object/function/etc. in ES6?










0















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










share|improve this question

















  • 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 affecting varIWantToCopy

    – Doug Coburn
    Nov 14 '18 at 1:19















0















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










share|improve this question

















  • 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 affecting varIWantToCopy

    – Doug Coburn
    Nov 14 '18 at 1:19













0












0








0








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:16









Harry SolovayHarry 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 do let clone = varIWantToCopy and then you can modify clone without affecting varIWantToCopy

    – Doug Coburn
    Nov 14 '18 at 1:19












  • 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 affecting varIWantToCopy

    – 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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.






share|improve this answer

























  • 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















1














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.






share|improve this answer

























  • 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













1












1








1







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 12:54

























answered Nov 14 '18 at 2:01









xeitonxeiton

2666




2666












  • 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

















  • 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
















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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto