javascript pass by value
function func(obj) {
obj = JSON.parse(JSON.stringify(obj)); //If too slow, replace with other method of deep cloning
obj.a += 10;
return obj.a;
}
var myObj = {a: 5};
func(myObj); //Returns 15 and myObj.a is still 5
javascript pass by value
function func(obj) {
obj = JSON.parse(JSON.stringify(obj)); //If too slow, replace with other method of deep cloning
obj.a += 10;
return obj.a;
}
var myObj = {a: 5};
func(myObj); //Returns 15 and myObj.a is still 5
are parameters modified in javascript
//Normal variable, No.
function square(x) {
x = x * x;
return x;
}
var y = 10;
var result = square(y);
console.log(y); // 10 -- no change
console.log(result); // 100
//Objects like struct sub variables, Yes.
function turnOn(machine) {
machine.isOn = true;
}
var computer = {
isOn: false
};
turnOn(computer);
console.log(computer.isOn); // true;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us