Connecting Tech Pros Worldwide Forums | Help | Site Map

Assigment operator in JavaScript.

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: Aug 21 '08
Have a look at my code snippet ....
Expand|Select|Wrap|Line Numbers
  1. function test(){
  2.     var obj = new Object();
  3.     obj.test = "Debasis Jana";
  4.     alert(obj.test);
  5.     var obj1 = obj;
  6.     obj1.test = "DMJPRO";
  7.     alert(obj.test);
  8. }
  9.  
Here assignment operates to copy the reference only. So changes to obj1 object makes changes to obj object.
But what i saw ...that if i copy the reference of a variable of child window to parent window variable ..after closing the child window how the parent variable exists ...?
i think after closing the window gets closed not the variables which bound to that window ... :-)

Debasis Jana
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Aug 21 '08

re: Assigment operator in JavaScript.


You have been here long enough to know that Java != Javascript.

<Moved>
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: Aug 21 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by r035198x

You have been here long enough to know that Java != Javascript.

<Moved>

Sorry here i mistyped.... :-)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#4: Aug 21 '08

re: Assigment operator in JavaScript.


yes ... objects and arrays are passed by reference (simple types like boolean, strings, numbers and even functions are passed by value) ... and the reference points to the memory address ... all is 'bound' to the 'base-parent' window (not really but that is your current runtime environment if you like to see it that way), so if you store/pass a reference to something to this window then the garbage collector just knows that you have a reference to it and ignores that reference instead of throwing it ;)

kind regards
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#5: Aug 21 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by gits

yes ... objects and arrays are passed by reference (simple types like boolean, strings, numbers and even functions are passed by value) ... and the reference points to the memory address ... all is 'bound' to the 'base-parent' window (not really but that is your current runtime environment if you like to see it that way), so if you store/pass a reference to something to this window then the garbage collector just knows that you have a reference to it and ignores that reference instead of throwing it ;)

kind regards

How functions and strings are passed by value ..can you give me some examples ?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#6: Aug 22 '08

re: Assigment operator in JavaScript.


for example with the assignment-operator (=) you pass a value or in case of javascript's arrays and objects a reference to those objects ... you might have a look here ... for getting an idea of it. it is even good to know when you use AJAX-callbacks where you may closure the reference to an object and easyly load data in the background to a js-business-object/component that you might have implemented.

kind regards
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#7: Aug 24 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by gits

yes ... objects and arrays are passed by reference (simple types like boolean, strings, numbers and even functions are passed by value) ... and the reference points to the memory address ... all is 'bound' to the 'base-parent' window (not really but that is your current runtime environment if you like to see it that way), so if you store/pass a reference to something to this window then the garbage collector just knows that you have a reference to it and ignores that reference instead of throwing it ;)

kind regards

If i pass a reference (bound to child window) to parent window and in the mean time i dereference the child window by set null to it then what would happen to that reference as child window reference set to null?
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#8: Aug 24 '08

re: Assigment operator in JavaScript.


Quote:
it is even good to know when you use AJAX-callbacks where you may closure the reference to an object and easyly load data in the background to a js-business-object/component that you might have implemented.

kind regards
The fundamentals of closure makes a function to be passed as value not by reference? Actually the reference of function passed and the copy of local variables of the called function gets copied with the closure to work the inner function properly. Please explain here.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#9: Aug 24 '08

re: Assigment operator in JavaScript.


Gits ... Tell me how functions & Strings are passed by Value ..please give me an example.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#10: Aug 24 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by dmjpro

Gits ... Tell me how functions & Strings are passed by Value ..please give me an example.

Recently I tested one thing ....
Expand|Select|Wrap|Line Numbers
  1. var str = "Debasis Jana";
  2. str[0] = 'S';
  3. alert(str[0]); //It still prints D
  4.  
Could you explain ?
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#11: Aug 24 '08

re: Assigment operator in JavaScript.


Recently I went through a Site i found two links ....
The first one i think it's right ...
See these two links ..
[Removed link to competing forum]
Link 2

Please experts have a look ..And throw some comments.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#12: Aug 24 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by dmjpro

Gits ... Tell me how functions & Strings are passed by Value ..please give me an example.

??? there is enough example in the article link i showed you ... ? what do you exactly want to know? the links you showed in your last post seem to be quite outdated ... currently it is like i already told you ...

kind regards

PS: a better discussion is to be found here
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#13: Aug 25 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by dmjpro

Recently I tested one thing ....

Expand|Select|Wrap|Line Numbers
  1. var str = "Debasis Jana";
  2. str[0] = 'S';
  3. alert(str[0]); //It still prints D
  4.  
Could you explain ?

How it works ..please tell me.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#14: Aug 25 '08

re: Assigment operator in JavaScript.


with the array-like notation you could just read the character at the specified position - as you could do with charAt() ... have a look here for more information - you cannot set somethig that way. you could use other methods to alter specific characters of a string like replace() or explode the string to a real! array and then rejoin the characters with join() ... etc.

kind regards
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#15: Aug 25 '08

re: Assigment operator in JavaScript.


Quote:

Originally Posted by gits

with the array-like notation you could just read the character at the specified position - as you could do with charAt() ... have a look here for more information - you cannot set somethig that way. you could use other methods to alter specific characters of a string like replace() or explode the string to a real! array and then rejoin the characters with join() ... etc.

kind regards

So String(char array) is immutable :-)
Reply


Similar JavaScript / Ajax / DHTML bytes