View Single Post
Old 12-04-2012, 09:33 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
All the above true. I wasn't trying to be comprehensive.

One thing none of us mentioned but that tends to confuse the heck out of people new to this stuff:

Variables are *NOT* and *CAN NOT* be objects!

Variables *CAN* be (and commonly are) *REFERENCES* to objects.

I was careful in my prior post when I wrote
Quote:
Do you understand that? That says "create an object, referenced by the variable john, with the properties shown.
Even when we do
Code:
    var john = new Person("john doe","john@xyz.com","Chief Gofer");
or even if we were to write (legal code!)
Code:
    var object = new Object( );
those variables (john and object) are *NOT* objects. They are REFERENCES to objects.

It's a very important distinction! If I now write
Code:
   var jane = john;
   jane.name = "Jane Roe";
   alert( john.name );
I will find that john has changed his name to Jane Roe!!!

Because all that var jane = john did was copy the *reference* to the object. So we ended up with two references to the same object.

So don't mistake references for actual objects. We treat them much the same, but they aren't the same.

********************

FWIW, this same distinction applies in Java and C# and, for the most part, in C++. (There are ways for a variable to actually *BE* an object in C++. Not commonly used, but...)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote