function update(o) {
o.a = o.a + 1;
o = { a: 100 };
}
>const obj = {a:1}
>update(obj)
console.log(obj)
what is the result ?
it would be 2.
> console.log(obj.a)
1
note that
even if the object is const value, the object keys are not protected.
in the function update(o), first line of o.a is still seeing the value of the obj, which is out of the scope of the function,
then second line, o = {a:100} , which is replacing o in the scope of function to a new object, however obj in the outside of the function is not changed.
0 件のコメント:
コメントを投稿