473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

JavaScript assign by value vs reference

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

Jun 1 '08 #1
5 8051
VK
On Jun 1, 11:30 am, howa <howac...@gmail.comwrote:
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.

Jun 1 '08 #2
howa wrote:
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 1 '08 #3
On 6$B7n(B1$BF|(B, $B2<8a(B7$B;~(B06$BJ,(B, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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.

Jun 1 '08 #4
howa wrote:
On 6月1日, 下午7時06分, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>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.
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 1 '08 #5
howa wrote:
Thomas 'PointedEars' Lahn wrote:
>You can only "copy" references to objects as you "copy" any
other value; you cannot "copy" the object itself. ...
<snip>
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.

Jun 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andy Fish | last post by:
Hi, can anybody put forward a sensible argument javascript's behaviour of creating a new global variable whenever I assign to a previously undeclared variable. I can't beleive this is just for...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
3
by: willwade | last post by:
Hi there, Sorry for the perhaps undirected post - Ive struggled for the past few hours and cant see the wood for the trees.. I have a page (infact a site but I will keep it simple) that is a...
3
by: umdsasha | last post by:
So, basically, I need to detect whether an alert window was thrown. I can't find where it's thrown from but I need to disable a button only if there were no alert windows thrown. Any ideas? ...
13
by: Oleg Konovalov | last post by:
Hi, I am working on a web application which among other things uses DHTML, Java and Javascript. It populates web page based on the contents of the database (resultset), and next to each row...
6
by: joe | last post by:
I need a few clarifications on how Javascript deals with arrays and Date object. Lets say daz is a date object. If I do this: somevar=daz; then "somevar" with be a pointer to daz, ie if I...
5
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.