0

I have a question about arguments passing in Javascript, as I know that, for primitive data types, when passing through a function, they got copied by value.

Haw about the reference data types?

As an example, see this code:

var person3 = new Object();
setName(person3);
console.log(person3)
function setName(obj){
  obj.Name = "remon"
}

For whom said that it is duplicate, it is not because I am asking the question from memory perspective and I am new in learning programming and the answers to that questions is so advanced for me.

9

1 Answer 1

0

@MinaShaker well, your experiment shows, that it is copied by reference. We see {Name: 'remon'} in console.

var person3 = new Object();
setName(person3);
console.log(person3)
function setName(obj){
  obj.Name = "remon"
}

And here, if we 'copy' object before sending it to the function we will get {Name: 'simon'}

var person3 = { Name: "simon" };
setName(Object.assign({}, person3));
console.log(person3)
function setName(obj){
  obj.Name = "remon"
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.