I always find it great to further your understanding by taking time to reflect on what you've been learning, so I'll do that here and hopefully someone can pick up something of value in the process.
I'm spending a bit of time reading up on Java. It's quite interesting comparing and contrasting Java and Javascript. Java is much more verbose in that you have to declare types, but on the other hand, to emulate certain features such as "private methods and properties", you have to do some long, drawn out procedure such as using closures.
In Javascript, there's no need to structure everything in terms of classes. In fact, up to ES5 (Ecmascript 5) which is the formal standard Javascript is based on, there are no classes. Everything is based upon prototypal inheritance, which involves 'chaining' objects together. You can emulate class-based OO in Javascript, but it's best to embrace prototypal inheritance for what it is.
In Javascript, object constructors are just regular functions which you use the 'new' keyword in front of when you create an instance of an object. Creating an instance of an object, or in other words 'instantiating' an object, means that you take a constructor, which is essentially an object template/blueprint, and create an actual object based off of it... For example, let's start off with a Car constructor. The purpose of a constructor is to help create objects of a similar kind. It can be tedious doing something like
var porshe;
porshe.wheels = 4;
porshe.speed = 40;
var camaro;
camaro.wheels = 4;
camaro.speed = 20;
Instead, you can use a constructor and make the process much simpler. Here is the constructor:
function Car (wheels, speed)
{
this.wheels = wheels;
this.speed = speed;
}
Then you create instances of the object by calling this constructor function
var ferrari = new Car(4, 20);
This is equivalent to
var ferrari;
ferrari.wheels = 4;
ferrari.speed = 20;
As you can imagine, when the list of properties similar objects have gets longer, using constructors is very handy. How all of this would be setup in Java would be quiet different.I'll speak more on that as I get more proficient with Java. But let's continue on to prototypes. Each constructor has a prototype property, which is an object, which you use to share properties and methods between all objects instantiated from that constructor... Let's take the constructor we used above and revisit it.
function Car (wheels, speed)
{
this.wheels = wheels;
this.speed = speed;
}
Now, you can alter the prototype property of Car by typing in individual properties or by passing it an object... for example:
Car.prototype.drive = function() {
console.log(this.car + "is driving.");
}
Car.prototype.color = "blue";
Now when an object is instantiated from a constructor, the methods and properties it receives from the constructor is exclusive to that object. On the other hand,methods and properties it gains from the prototype are shared between all objects from that particular constructor. If you change the prototype property, ALL objects that reference that prototype will be changed.