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
//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;
Also in JavaScript:
- moment format datetime postgresql
- jquery wrap inner text
- react project ideas
- javascript find document body
- round to decimal javascript
- reactjs facebook login popup trigger on load page
- React setup for handling UI.
- nodejs print
- closure javascript
- node dotenv
- react router catch all 404
- javascript join list of string
- Material-ui wallet icon
- route parammap subscribe angular 9
- manual avanzado java script
- new line with javascript write
- how to access data in json format using asp.net c#
- javascript select all table rows
- js maximum number value
- remove property from javascript object
- react hero slider
- Mobx-react console warning related observer
- Javascript get random item from array
- quill js server side delta