I not too deep into JavaScript. Therefore have done this tinkering:
var demo = 'test';
var obj = {
x: 'abc'
}
function foo(str) {
str += '_123';
return str;
}
var ret = foo(demo);
var retObj = foo(obj.x);
console.log('Result: %s', ret); // => 'test_123'
console.log('Used parameter: %s', demo); // 'test'
console.log('Result: %s', retObj); // 'abc_123'
// The value of the property is still unchanged. So
// it has been passed in as by value. As as copy.
console.log('Used parameter: %s', obj.x); // 'abc'
I pass a string to the function. One time as primitive variable and the next time as object property.
Afterward the original data are unchanged. => The outer system is unchanged.
Now I am wondering:
Why do they have these immutability feature in React?
I mean: They say a function shall do something and provide as result. But not changing the system as whole.
But if data are passed as by value anyway? Why do the make such a big thing out of having immutability?