473,395 Members | 1,869 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.

Functions and Objects.

dmjpro
2,476 2GB
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 ?
Aug 24 '08 #1
28 1461
gits
5,390 Expert Mod 4TB
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
Aug 24 '08 #2
dmjpro
2,476 2GB
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.  
Aug 25 '08 #3
rnd me
427 Expert 256MB
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.  
Aug 25 '08 #4
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #5
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #6
dmjpro
2,476 2GB
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" ?
Aug 25 '08 #7
dmjpro
2,476 2GB
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?
Aug 25 '08 #8
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #9
RamananKalirajan
608 512MB
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
Aug 25 '08 #10
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #11
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #12
dmjpro
2,476 2GB
link = reference

kind regards
So Rnd is wrong or something else ???
Aug 25 '08 #13
gits
5,390 Expert Mod 4TB
no ... i was wrong in saying that it would be a copy ...
Aug 25 '08 #14
dmjpro
2,476 2GB
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 ????
Aug 25 '08 #15
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #16
rnd me
427 Expert 256MB
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.
Aug 25 '08 #17
dmjpro
2,476 2GB
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 . :-)
Aug 25 '08 #18
gits
5,390 Expert Mod 4TB
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' ... ?
Aug 25 '08 #19
gits
5,390 Expert Mod 4TB
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
Aug 25 '08 #20
rnd me
427 Expert 256MB
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.
Aug 25 '08 #21
rnd me
427 Expert 256MB
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...
Aug 25 '08 #22
gits
5,390 Expert Mod 4TB
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
Aug 26 '08 #23
dmjpro
2,476 2GB
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 :-)
Aug 26 '08 #24
gits
5,390 Expert Mod 4TB
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
Aug 28 '08 #25
dmjpro
2,476 2GB
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 ;)
Aug 28 '08 #26
gits
5,390 Expert Mod 4TB
he did behind the scenes ;) ... but that's the mod's secret ... :)
Aug 28 '08 #27
dmjpro
2,476 2GB
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
Aug 28 '08 #28
acoder
16,027 Expert Mod 8TB
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.
Aug 28 '08 #29

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
9
by: relaxedrob | last post by:
Howdy All! I am still getting my head around a few base concepts. Any comments or criticisms on the below definitions would be most welcome! A function is an object. JavaScript objects have...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.