Connecting Tech Pros Worldwide Forums | Help | Site Map

Functions and Objects.

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: Aug 24 '08
Have a look at this code.
Expand|Select|Wrap|Line Numbers
  1. function test(){
  2. //some code
  3. }
  4. a = test;
  5. a = new test();
  6.  
What is the difference between these two statements ?

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Aug 24 '08

re: Functions and Objects.


the first creates a copy of your constructor ... the second creates a new instance of the object test ... with the first you you could even do:

Expand|Select|Wrap|Line Numbers
  1. var b = new a;
kind regards
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by gits

the first creates a copy of your constructor ... the second creates a new instance of the object test ... with the first you you could even do:

Expand|Select|Wrap|Line Numbers
  1. var b = new a;
kind regards

Expand|Select|Wrap|Line Numbers
  1. var a = new test;  //it is similar as creating a new instance of zero argument constructor.
  2.  
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#4: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by gits

the first creates a copy of your constructor...

technically, it is not copied, you create a link to the function object.

OP:

when the "new" keyword is used, the constructor's prototype is bound to the returned object's prototype.

remember that function are objects.

consider what happens when you create a constructor function and add prototype methods later :


Expand|Select|Wrap|Line Numbers
  1. function Person(name){ this.bornOn = new Date; this.name=name; return this; }
  2. Person.prototype.getAge = function(){ return (new Date()).getTime() - this.bornOn.getTime(); }
  3.  
what you are doing is tacking methods onto the "Person" object, which happens to be function, aka the constructor.

later, when "new" is used while calling Person(), the methods you tacked onto the "Person" object (aka the constructor function), are bound to the new person instance object returned by the constructor.

remember, functions are objects. what you do above is comparable to this example:

Expand|Select|Wrap|Line Numbers
  1. Object.prototype.c = 3;
  2. var ob = new Object( {a:1,b:2});
  3. alert(ob.c); // shows:  3
  4.  
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#5: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by rnd me

technically, it is not copied, you create a link to the function object.

thanks for pointing that out :) ... to see that in action have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. function copy_f(f) {
  2.     return f;
  3. }
  4.  
  5. function copy_a(a) {
  6.     return a;
  7. }
  8.  
  9. function foo() {
  10.     this.bar = 'test';
  11.     this.a   = [1, 2];
  12.  
  13.     this.b   = copy_a(this.a);
  14. }
  15.  
  16. foo.prototype.foobar = function() {
  17.     alert(this.bar);
  18. }
  19.  
  20. foo.prototype.c = copy_f(foo.prototype.foobar);
  21.  
  22. foo.prototype.foobar = function() {
  23.     alert('test_test');
  24. }
  25.  
  26. var a  = new foo;
  27. a.b[0] = 2;
  28.  
  29. a.foobar();
  30. a.c();
  31.  
  32. alert(a.a[0]);
  33.  
we declare a method foobar ... pass it through a function and so copy it to c ...
then we overwrite foobar and we see that c is the original method while foobar is the new version of the function ... while passing the array 'this.a' just passes the reference, so when we set a.b[0] = 2; later on then a.a[0] is set accordingly to 2, that means: copy_a doesn't really copy something. that is what i meant ... functions and primitive datatypes are passed by value ... while objects and arrays are passed by reference.

kind regards
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#6: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by dmjpro

Expand|Select|Wrap|Line Numbers
  1. var a = new test;  //it is similar as creating a new instance of zero argument constructor.
  2.  

in case the constructor doesn't get any argument passed to it then we could just leave out the parenthesis, because we don't need to enforce the interpreter to eval an empty arguments-list ...

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

re: Functions and Objects.


Quote:

Originally Posted by rnd me

technically, it is not copied, you create a link to the function object.

What does it actually mean ?
What Gits shown that the functions and basic data types passed by value.
So what do you mean that "Technically it does not copy it just a link to that function" ?
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#8: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by rnd me

Expand|Select|Wrap|Line Numbers
  1. var ob = new Object( {a:1,b:2});
  2.  

How to access a & b using object "ob"?
What does this JSON object "{a:1,b:2}" mean?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#9: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by dmjpro

What does it actually mean ?
What Gits shown that the functions and basic data types passed by value.
So what do you mean that "Technically it does not copy it just a link to that function" ?

link = reference

kind regards
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 350
#10: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by dmjpro

How to access a & b using object "ob"?
What does this JSON object "{a:1,b:2}" mean?


[HTML]<html>
<head>
<script type="text/javascript">
var ob = new Object( {a:1,b:2});
function doThis()
{
alert(ob.a);
alert(ob.b);
}
</script>
</head>
<body>
<input type="button" value="Alert Value" onclick="doThis()">
</body>
</html>[/HTML]

sorry I am not having so much idea on JSON, what I know is it used for two way data exchange in Ajax. I havent used much with the JSON

Regards
Ramanan Kalirajan
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#11: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by dmjpro

How to access a & b using object "ob"?
What does this JSON object "{a:1,b:2}" mean?

it creates a new Object with the properties a and b ... you could even just use the literal creation like in the example below. the native Object is extended with a property c. new Object just wraps the value that is passed to it and this is an object itself.

Expand|Select|Wrap|Line Numbers
  1. Object.prototype.c = 3;
  2.  
  3. var ob = { a: 1, b: 2 };
  4.  
  5. alert(ob.a);
when you do in firefox:

Expand|Select|Wrap|Line Numbers
  1. var ob = new Object({});
  2.  
  3. alert(ob.constructor.name);
you will get 'Object' but when you do:

Expand|Select|Wrap|Line Numbers
  1. var ob = new Object([]);
  2.  
  3. alert(ob.constructor.name);
you'll get 'Array'

kind regards
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#12: Aug 25 '08

re: Functions and Objects.


and just to clarify the word JSON: it is nothing special when using it in JavaScript itself ... as you see it just uses the literal notation of javascript objects and so you may! transfer data as strings that when they are evaled you just have native javascript objects ... that are ready to use. there is no magic ... JSON basicly is just JavaScript code so that there is no need to parse XML or whatever to extract data from other transfer-formats ...

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

re: Functions and Objects.


Quote:

Originally Posted by gits

link = reference

kind regards

So Rnd is wrong or something else ???
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#14: Aug 25 '08

re: Functions and Objects.


no ... i was wrong in saying that it would be a copy ...
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#15: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by gits

no ... i was wrong in saying that it would be a copy ...

I don't understand .... how ?
If then passing of functions by reference or value ????
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#16: Aug 25 '08

re: Functions and Objects.


try to follow post #5 ... there you have both. when functions passed as parameters, then they are passed by value ... when you just assign the reference to a variable by the function name then they are referenced ... while arrays and objects are always passed by reference.

kind regards
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#17: Aug 25 '08

re: Functions and Objects.


functions are objects, and as such passed be reference.

note that assigning a variable to the result of a calling function can result in a reference or a value, depending upon what type of object the function returns.

numbers, text, true, false, undefined, NaN, and null are passed by value, everything else is passed as a reference.

think of a file-system analogy:
primitives are copied as when dragging an mp3 file from a flash drive to the desktop.
applications don't copy on a drag: like javascript objects, create an alias (shortcut/ref) on the desktop.

the reasons for the default behavior of both is related.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#18: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by gits

try to follow post #5 ... there you have both. when functions passed as parameters, then they are passed by value ... when you just assign the reference to a variable by the function name then they are referenced ... while arrays and objects are always passed by reference.

kind regards

Now I understand the points . :-)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#19: Aug 25 '08

re: Functions and Objects.


Quote:

Originally Posted by rnd me

functions are objects, and as such passed be reference.

typically i would agree, but as you could see from post #5 a function is passed through the copy_f method and is copied that way ... while arrays and objects stay referenced as the copy_a method shows ... i have to admit, that i'm a little bit confused right now ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. function FOO() {
  2. }
  3.  
  4. FOO.prototype.c = function() {
  5.     alert('foo'); 
  6. }
  7.  
  8. var a = FOO;
  9.  
  10. var b = FOO.prototype.c;
  11.  
  12. a.prototype.c = function() {
  13.     alert('123');
  14. }
  15.  
  16. var o = new FOO;
  17.  
  18. o.c();
  19. b();
  20.  
here is b a copy instead of a reference ... so it seems to be a difference how a function is declared ... since it is a function too, but calling b alerts 'foo' while calling o.c alerts '123' ... ?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#20: Aug 25 '08

re: Functions and Objects.


hmmm ... unless i missed something it seems not to be quite clear how this works. in post #5 i think the function is passed by value since it is closured through the copy_f method there. but why is the method c assigned by value to var b in the above example while FOO is assigned by reference to variable a? it seems to be that an objects property that is a function is passed by value while an object itself is passed by reference. When i do:

Expand|Select|Wrap|Line Numbers
  1. function FOO() {
  2.     this.c = function() { alert('foo') }
  3. }
  4.  
  5. var a = FOO;
  6.  
  7. var o = new FOO;
  8.  
  9. o.c(); // alerts 'foo'
  10.  
  11. var b = o.c; // copies the function again like in the previous post
  12.  
  13. o.c = 'test'; // assigns a string instead of the function to method c of o
  14.  
  15. b(); // alerts 'foo' as expected from a copy
  16.  
  17. o.c(); // throws an error as expected ... c is no function anymore
  18.  
kind regards
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#21: Aug 25 '08

re: Functions and Objects.


wow. i may be (at least partially) wrong about this...

not sure what to make of the example in post #20.

this is surprising to me, mostly because it's never come up before.

a simpler example:

Expand|Select|Wrap|Line Numbers
  1. function foo () {
  2.   alert(300)
  3. }
  4.  
  5. var b = foo;
  6. foo="clobbered";
  7.  
  8. [foo.toSource(), b.toSource()]
  9.     //=["(new String("clobbered"))", "function foo() {alert(300);}"]
  10.  
which shows that foo is being passed by value....


there has to be a rule about when each behavior occurs, funny i have never come across it.

i will do some research and post back as official answer as i can find.
rnd me's Avatar
Expert
 
Join Date: Jun 2007
Location: Urbana IL
Posts: 411
#22: Aug 26 '08

re: Functions and Objects.


update:
according to current msdn jscript documents , it says always by ref. however, an older archive version of the jscript doc i have says "Objects, arrays, and functions are copied, passed, and compared by reference under most circumstances." (emphasis added).

as we demonstrated in the last post, M$'s current documentation is wrong.
not always, but sometimes.

what is going on here?
when is that sometimes?

more on this later...
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#23: Aug 26 '08

re: Functions and Objects.


hmmm ... we tried something and think we could have a kind of explaination. have a look at this, i use an array since arrays are always passed by reference for sure :) ... this example is quite similar to our above examples.

Expand|Select|Wrap|Line Numbers
  1. var a = [1, 2, 3];
  2.  
  3. var b = a;
  4.  
  5. a = {};
  6.  
  7. alert(b.toSource());
  8.  
the last assignment to a drops its reference but b points to the 'old' array and while javascript always counts the references to objects/arrays/functions it seems to 'closure' this old ref so that a just seem to be a new reference and b stores the old one... so the problem is just the assignment.

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

re: Functions and Objects.


I test this code snippet ..and have the expected Output .... :-)
Expand|Select|Wrap|Line Numbers
  1. function Object1(){
  2. }
  3.  
  4. function Object2(){
  5. }
  6.  
  7. function test(){
  8.     var a = Object1;
  9.     a.prototype.prop1 = 100;
  10.     var b = a;
  11.     b.prototype.prop1 = 200;
  12.     alert(new a().prop1);
  13. }
  14.  
Thanks Gits and Thanks Rnd Me :-)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#25: Aug 28 '08

re: Functions and Objects.


i'll post a link to an article(-draft) when i have one ready (probably at the weekend) ... currently it seems to work the following way:

while the simple types are passed by value itself, arrays, objects and function-objects are getting passed by its reference values ... as long as you don't assign a new object to a variable that holds this value this variable points to the original object - so when you change this original object the variable points to the changed object too ... what is often a bit misunderstood as passed by reference ... including me :)

i think an article should compile some things out of this thread so that we could clarify this issue in one article ... because it doesn't seem to be a real 'in one place' information about this issue out there ...

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

re: Functions and Objects.


Quote:

Originally Posted by gits

i'll post a link to an article(-draft) when i have one ready (probably at the weekend) ... currently it seems to work the following way:

while the simple types are passed by value itself, arrays, objects and function-objects are getting passed by its reference values ... as long as you don't assign a new object to a variable that holds this value this variable points to the original object - so when you change this original object the variable points to the changed object too ... what is often a bit misunderstood as passed by reference ... including me :)

i think an article should compile some things out of this thread so that we could clarify this issue in one article ... because it doesn't seem to be a real 'in one place' information about this issue out there ...

kind regards,
gits


Yeah i ll be waiting for that ....
And one more thing what i_am_clint posted ..the offset links ...those are really good ..and handy ...
anyway you post that artical i ll have a look at it ..because with out the core things the JavaScript is meaning less ..when we design an algorithm for other language we should be using it in JavaScript also ....:P

I also invited Acoder to take part in this discussion but he didn't ;)
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#27: Aug 28 '08

re: Functions and Objects.


he did behind the scenes ;) ... but that's the mod's secret ... :)
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#28: Aug 28 '08

re: Functions and Objects.


Quote:

Originally Posted by gits

he did behind the scenes ;) ... but that's the mod's secret ... :)

About whom you are talking? I am talking about Acoder not PB MOD ..;)
One more thing i talked to Acoder and he replied me that he should clarify something before he could contribute in this discussion ;)

Have a look at here
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#29: Aug 28 '08

re: Functions and Objects.


You talking about me? Well, here I am.

I'll endorse what gits has already stated. JavaScript passes by value, not by reference. It seems that non-literals/objects are passed by reference, but it's reference by value. Many are confused by this and get their terminology in a muddle.
Reply