Javascript's assignment operation is to copy r

2020-03-18 17:12发布

The basic example:

var b = 10;
var c = b;
b++;
console.log(b,c);

>> 11 10

c looks like a copy of b.

But in another case:

var x = {};
var y = x;
x.abc = 10;
console.log(x.abc, y.abc);

>> 10 10

Why is the y not a copy of x, but a reference which points to the same instance x points to?

Also, I guessed b++ creates another instance, so b points to the new instance but c points to the old one. However...

var u = 10;
setTimeout(function() {
  console.log(u);
}, 10000)
u++;

>> 11

If u++ creates a new instance, then the u inside the anonymous function should point to the old u, shouldn't it?

4条回答
Viruses.
2楼-- · 2020-03-18 17:41
  • The typeof value assigned to a variable decides whether the value is stored with assign-by-value or assign-by-reference

  • On variable assignment, the scalar primitive values (Number, String, Boolean, undefined, null, Symbol) are assigned-by-value and compound values (Object, Array) are assigned-by-reference

If there are doubts look into this its explained with examples Link: how references work

查看更多
Lonely孤独者°
3楼-- · 2020-03-18 17:59

When primitives are assigned, they are assigned by value; references types (like your object) are assigned by reference (or, as Jon Skeet corrects me, they're assigned a copy of the reference).

In your second example x and y both point to the same object in memory. That's why adding an abc property to one, also adds it to the other

You'd also observe the same behavior passing x or y into a function

function addABC(foo) {
   foo.abc = 10;
}

var x = {};
var y = x;
addABC(x);
console.log(x.abc, y.abc);

Just note that, though x and y point to the same object in memory, they're separate copies of the reference, so this

    var x = { a: 1 };
    var y = x;
    y = {};
    alert(x.a);

and this

    var x = { a: 1 };
    var y = x;
    x = {};
    alert(y.a);

will still alert 1.

查看更多
冷血范
4楼-- · 2020-03-18 18:05

the c looks like a copy of b.

Both are references to the same immutable value.

Why the y is not copy of x but a reference which points to the instance x points to?

x was a reference to an object in the first place, so y is a copy of it (a copy of the reference, not a copy of the object).

If u++ creates a new instance,

It doesn't.

the u in anonymous function should points to the old u, shouldn't it?

u++ assigns a reference to 11 to u. The anonymous function is looking at u and not "the value of u at the time the function was created".

查看更多
等我变得足够好
5楼-- · 2020-03-18 18:06

This statement:

var y = x;

copies the value of x as the initial value of y. However, the values involved are references to an object, not the object itself. Note that this is not the same as saying that the assignment copies "a reference to x" - it really is the value of x. So in particular, if you change the value of x to refer to a different object, e.g.

x = "something else";

then that won't change the value of y - its value will still be a reference to the original object.

查看更多
登录 后发表回答