JavaScript assign by value vs reference

H

howa

Hi,

Consider a simple example, e.g.

var a = {

'a': 'b',
'c': 'd'

}

var k = a;


1. Is it assign by value (copying) or by reference)
2. If it is by value/reference, how to make them assign by reference/
value

Howard
 
V

VK

Hi,

Consider a simple example, e.g.

var a = {

'a': 'b',
'c': 'd'

}

var k = a;

1. Is it assign by value (copying) or by reference)

Why not to check by yourself?

var a = {
'a': 'b',
'c': 'd'
}

var k = a;

k.foo = 'bar';

window.alert(a.foo); // 'bar'

k gets a copy of reference held by a
2. If it is by value/reference, how to make them assign by reference/
value

most libraries are having custom clone() method or equivalents. AFAIK
it is mainly based on properties iteration and assigning to a new
object - unless it is an instanceof Array where slice/splice trick is
much more effective.
 
T

Thomas 'PointedEars' Lahn

howa said:
Consider a simple example, e.g.

var a = {

'a': 'b',
'c': 'd'

}

This is initializing an Object object and assigning the reference to it to `a'.
var k = a;


1. Is it assign by value (copying) or by reference)

As (object) references are values in ECMAScript implementations, you are
asking the wrong question.

You are assigning the reference value stored in `a' to `k' here. So after
that assignment, `k' refers to the same object as `a'.
2. If it is by value/reference, how to make them assign by reference/
value

You can only "copy" references to objects as you "copy" any other value; you
cannot "copy" the object itself. However, you can "copy" certain property
values from one object to another, or you can have one object inherit
properties from another through the prototype chain. But that is different
from copying the object; objects have identity.


PointedEars
 
H

howa

You can only "copy" references to objects as you "copy" any other value; you
cannot "copy" the object itself. However, you can "copy" certain property
values from one object to another, or you can have one object inherit
properties from another through the prototype chain. But that is different
from copying the object; objects have identity.

Thanks.

So it is same as Java then.

primitive data type are assign by value, object are assign by value
reference.
 
T

Thomas 'PointedEars' Lahn

howa said:

You are welcome.
So it is same as Java then.

Not quite. Java uses class-based, not prototype-based, inheritance that
does not allow one object to inherit from another.
primitive data type are assign by value, object are assign by value
reference.

No. Primitive values and object references are values, period.

"Assign by reference/value" also does not strike me as being a reasonable
term. There are "call by value" and "call by reference" but those terms
apply neither to ECMAScript implementations nor Java.


PointedEars
 
R

Richard Cornford

So it is same as Java then.

primitive data type are assign by value, object are assign
by value reference.

It is safe to behave as if 'primitive data types are assigned (and
passed) by value', but they may not be. Javascript/ECMAScript has no
operations/operators that can modify a primitive data type so it would
be entirely possible for an implementation to internally use objects as
its representations of primitive data types and be assigning references
to those objects whenever code assign a primitive value to a
variable/property. Indeed there may be good reasons for implementing
javascript in precisely that way (for one thing it removes the need for
the assignment process to care about the type of value it is handling).
However, because there are no mechanisms for directly altering the value
of a primitive data type there is no way you tell, and there are no
consequences; it does not matter.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top