How To Create "Classes" In JavaScript
Functions in JavaScripts are objects themselves and objects in JavaScript are associative arrays. An associative array is an abstract data type consisting of unique keys with associated values. Very simply put, this means that you can use the function in JavaScript as a class.
The point is to add keys and values inside the function. Then "new" the function and you have the behaviour of a class. You can reference your instance of the function and move it back and forth between methods. E.g. I use this in a script that connects to a database an retrieve some values that I store in my instance of the function.
function Car(brand, type) {
this.brand = brand;
this.type = type;
}
Proving that the function is indeed an associative array, try outputting the car example the following way:
var car = new Car("Toyota", "Aygo");
// Syntactic Sugar
alert("My car is a " + car.brand + " " + car.type);
// Associative Array
alert("My car is a " + car["brand"] + " " + car["type"]);
It will give you the same output.
A personal note: I think the JavaScript syntax in this matter is lacking a Class keyword. Having functions act as classes obfuscates the readability of the code. I think it is important to have a good coding convention for JavaScript.
Happy programming!