I need a little js help, I don't know what to sear...
# suitescript
t
I need a little js help, I don't know what to search for this. Problem is I think abc shouldn't be updated. But it is updating and I don't know the reason, so came here to know the reason.
b
key terms you should be looking for are
pass by value
vs
pass by reference
p
Some things are pass by value in JS, others reference. The 2nd answer here does a good job of explaining https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language
t
my one logic fits in here, but not sure if its correct. Address of object abc is passed instead of actual object. so the modification happens on the object placed at the location.
ohh
Okay, I forgot that. how will I do if I want to do pass by value here ?
b
basic answer is to use a primitive instead of an object
more direct is you cant use an object
t
Hmmm, I got it now, Thanks a lot for your help @battk and @PNJ
w
What are your preferred ways of copying an object? JSON.parse(JSON.stringify(theObject))?
a
@Watz The correct way to copy an object is by using
N/util
module.
util.extend({}, ObjectToBeCopy);
👍 2
w
Ah, intresting. Didn't know it had that.
b
its essentially Object.assign, with the same weakness that it isnt a deep clone
so if you are looking for deep clone, then you want _.cloneDeep
👍 1
s
Yeah I'm pretty sure that is exactly what the util is doing, looping first level properties and assigning
m
Or for shallow clone in SS2.1,
const clone = { ...objectToBoCopied };
👍 1
t
So that dumb question actually brought me some enlightenment 😀, Thanks you guys for these .