0

There seems to be some confusion on the definition of an instance, a reference and an object. Can someone explain the difference between those three in Javascript?

To make it simple, please do not use the word class/classes in your answer. Do not talk about java-- let's keep it to javascript-- please.

Some say that an instance is just a reference:

"Objects are things in memory while instances are things that reference to them."

see: Difference between object and instance

Other say that objects are instances, and then references are-- for example-- a variable that references that instance.

See: Difference between reference and instance in javascript

    iAmAnObjectInstance = {...object stuff...};

    var theReference = iAmAnObjectInstance;

so, which is it?

A. Objects are instances. References refer to the instance (of the object).

or

B. instances are references and references are instances. They reference where an object is in the memory/code/program.

Also, if anyone knows of a good annotated, plain English ontology for Javascript terms that would help explain these (not just the ECMAScript spec, but perhaps derived from it), that could be very useful too.

Community
  • 1
  • 1
A.com
  • 1,466
  • 4
  • 19
  • 31
  • 2
    The idea that a reference is an object instance is nonsense. – Pointy Dec 16 '15 at 22:25
  • 2
    Language is flexible and not always precise. Especially because these terms are used to describe a variety of languages, and different programmers have different experiences with them. – Barmar Dec 16 '15 at 22:25
  • 1
    You can't use the word "instance" without using the word "class". – Barmar Dec 16 '15 at 22:26
  • @Pointy, why does SO let that post sit in the open if it's blatantly wrong? I'm not a fan of authoritarian regulation... but if it's blatant....why why why? – A.com Dec 16 '15 at 22:28
  • @A.com which *blatantly wrong* "post" are you talking about? [This one](http://stackoverflow.com/a/3323705/1250301)? You'll notice that has considerably less up votes *because it is wrong* (and confusing). The [much better answer](http://stackoverflow.com/a/3323377/1250301) has lots of up votes. That's how it's supposed to work. – Matt Burland Dec 16 '15 at 22:30
  • @Barmar (1) you can definitely talk about a javascript instance without using the word class. Also, why would we not want a solid ontology for programming? (2) You're basically saying "programmers have never been precise in their language. Programmers used these words differently in the past, therefore we should never try to form a solid ontology." (3) This question was about javascript not other languages. That way, we cut down on the confusion. In Javascript.... what do these mean? I was pretty straight forward in my question that I didn't want to talk about other languages. (4) thanks – A.com Dec 16 '15 at 22:31
  • Maybe this would be a better question for cs.stackexchange.com. Ontology doesn't really affect day-to-day programming. – Barmar Dec 16 '15 at 22:32
  • @Barmar Really nailed it: the terms are vague because different programming languages do similar things in subtly different ways, and it's tough to come up with universally-applicable definitions. If you did come up with precise, encompassing terms, it's plausible nobody would use them because they'd be too complicated. JavaScript is special in having such a messed-up history that everything around it is even more confused than elsehwere. – Jeremy Dec 16 '15 at 22:32
  • 1
    @MattBurland, I am talking about this one: Difference between object and instance. If the information in it is blatantly wrong (as pointy suggests), it seems like something that should be gotten rid of... it's just going to confuse and waste people's time who come across it. – A.com Dec 16 '15 at 22:33
  • 3
    SO is for questions that have objective, well-defined answers. If there's not a consistent terminology in use, how do you expect someone to give a good answer to this question? This isn't the appropriate venue to try to start a movement to clean up the language. – Barmar Dec 16 '15 at 22:34
  • 1
    @JeremyBanks, I can respect barmar's point that programmers have been horrible about creating an ontology in the past. However, I am not sure why that means they should not do so moving forward. – A.com Dec 16 '15 at 22:35
  • 2
    @Barmar I'm not trying to start a movement. I was kind of hoping there was an answer to a pretty simple question. But I guess not. That's all I was wondering. Kind of surprising but okay. – A.com Dec 16 '15 at 22:36
  • I suspect that the language-design-ey parts of computer science academia may have precise terms for a lot of these things. But we code monkeys mostly ignore them. – Jeremy Dec 16 '15 at 22:37
  • The reason you felt the need to ask the question is because you observed that there isn't consistent use of the terminology. – Barmar Dec 16 '15 at 22:39
  • 1
    Please note how jfriendOO just succinctly answered the question. While others poo poo'd it and down voted me. Interesting. Is this par for the course on SO? – A.com Dec 16 '15 at 22:42
  • Nothing in any question or answer you linked suggests that a reference is itself, intrinsically, an instance. A reference is called a "reference" because it *refers to an instance*. It is not itself an instance. If you'd update your question with the specific language that led you to believe that somebody was claiming that a reference is itself an instance (of something), perhaps somebody could dispel your confusion. – Pointy Dec 17 '15 at 03:09
  • 1
    I voted to reopen because I think an objective answer that is not primarily opinion can be provided just fine. – jfriend00 Dec 17 '15 at 03:24
  • @jfriend00 maybe, but the question *really* needs to be cleaned up before reopening. – djechlin Dec 17 '15 at 03:30
  • @djechlin - I took a shot at cleaning up the question. What else do you think it needs? – jfriend00 Dec 17 '15 at 03:40

1 Answers1

6

In Javascript, a constructor function and a prototype definition form a process and definition for creating new objects with a known set of properties/methods.

Each time the constructor is called, a new object of that type is created. Each of one these new objects that is created is an instance of this type of object. When first constructed, it has a known set of properties/methods which give it a known behavior or set of values.

So, this creates a new instance of an object:

var foo = new myConstructor();

Each time you call that constructor, it creates yet another instance of that object.

In Javascript, a reference is a means of referring to that newly created object. You may have many references to that same object.

The instance is the actual object in memory.

A reference is some variable or argument or property that currently refers to or points to that instance.

Since there is no direct access to all instances in Javascript, one always has to have a reference to it in order to access it. And, in fact, this is how Javascript garbage collection works. When there are no longer any references to any object in live reachable code, then the instance of the object becomes eligible for garbage collection and can be disposed of.

You use a reference to access the object.

var test0 = new myConstructor();
var test1 = test0;
var test2 = test0;
var test3 = test2;

In Javascript, all four of the above variables all contain a reference to the same newly created object. For those familiar with other languages, a reference in Javascript in this case works more like a pointer than it does what other languages might call a reference. You can use that reference to modify the one common object:

var test0 = new myConstructor();
var test1 = test0;
test0.myProp = "something"
test1.myProp = "whatever";
console.log(test0.myProp);      // "whatever"
console.log(test1.myProp);      // "whatever"
console.log(test0 === test1);   // true

Both test0 and test1 refer to the same object so modifying a property or calling a method on the object reference in either variable operates on exactly the same object.

When using the term reference in a Javascript context, it does not work the same as that term does in a C++ context. For example, if you assign to the variable test0:

var test0 = new myConstructor();
var test1 = test0;
test0 = {greeting: "hello"};

This just puts a different object reference into test0 and has no effect at all on the object that test0 use to refer to or the one that test1 currently refers to. That's why I say that object references in Javascript behave more like pointers than references in other languages. I'm not trying to invent any Javascript lingo here, just to compare it with known features in other languages to explain how a Javascript reference works.


As for the questions/answers you referenced in your question.

This question you referenced asks:

Sometimes I hear people say "a reference to a object" and some say "a instance of a object" What is the difference?

And the selected answer says this:

A variable will hold a reference to an instance of an object.

The actual object is an instance.

The variable/variables used to access the object hold the references to it.

Which is pretty much what I said also. I added that a reference can be stored in a property or an argument as well as a regular variable.

The other question you referenced tries to answer a similar question without regards to any particular language and that becomes much more problematic because, as you have pointed out, all terms do not mean exactly the same thing in every computer language. So, that other question and its answers are conceptually constructive, but not specific to Javascript and thus not sufficient for Javascript.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979