473,756 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8085
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...@we b.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...@we b.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
6719
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 the sake of convenience (surely we learned this much from basic). here's my proposal: all "global" (document scope) variables must be declared by 'var' outside a function block.
5
2692
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 function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
6
5581
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 object collection and stanrd using it it starts to fall apart. Clearly there is something about javascript's usage of passing "By ref" that i am not getting. i have had a look on the web and found some examples, but i cant see why my code does not...
3
2659
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 css/table mix. One of the images is sitting in a pretty straightforward div: <td valign="top" width="391" align="left"> <div id="bigimage"><img
3
9695
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? Thanks Alex
13
39439
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 there is a checkbox (v1) allowing to select that row for changes (e.g. delete, update, etc.) So we are creating an array of checkbox, correct ? Of course I have to check whether any of these checkboxes exist and if any of them got selected (checked)
6
1832
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 change somevar later daz will change also. But If I do this: somevar=new Date(daz);
5
1022
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
0
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9271
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9838
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.