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.