I'm not an expert, I'm still learning but I'll try to explain it to you and if I'm wrong let someone correct me.
JavaScript is an Object based language. So you have objects like String, Array, Number, Math... All this objects have some properties and methods you can use.
But what if you want to build one of your own? Let's say you are building an application for some car shop. You want to be able to add buyer's details like for example:
- his/hers name and surname
- his/hers city/street address
- car model
- car price
- car color
etc...
JavaScript obviously doesn't come with an object that let's you do that (actually you can do it on number of different ways but pretend that you can't).
So you may define a reference type which we'll call simple "CustomerDetails()". What are reference types? Reference types are simply templates for your objects as arhitect's drawings are templates used to build a house. One thing to note here is that some developers refer to reference types as classes and use this two terms interchangeably. While this is correct for object-oriented languages such as C#, C++, Java... this is not correct for JavaScript as JavaScript has no formal class construct although it has support for reference types (logical equivalent to classes).
A reference type consist of three things:
- A constructor
- Method definitions
- Properties
How do you define it?
function CustomerDetails() {}
At first this may look like the same definition you are using when defining new functions. It's correct until you start to add some properties and methods to it.
So lets do that
function CustomerDetails()
{
var custName;
var custSurname:
var custCarModel;
var carPrice;
var carColor;
}
Now you have defined your template for CustomerDetails object. So how do you add properties and methods to it? Simply by using prototype property. Prototype property let's you add properties and methods to objects. So let's add some methods and properties to our CustomerDetails reference type.
CustomerDetails.prototype.setCustName = function(cName)
{
this.custName = cName;
}
CustomerDetails.prototype.setCustSur = function(cSurname)
{
this.custSurname = cSurname;
}
...
How do you get values from your variables?
CustomerDetails.prototype.getCustName = function()
{
return this.custName;
}
CustomerDetails.prototype.getCustSur = function()
{
return this.custSurname;
}
...
In the code above "this" keyword referes to that object instace of your reference type.
So now that you have defined all of your methods and properties you have to be aware of one more thing. JavaScript won't create an object until you say so.
How do you tell JavaScript to create an instace of object?
var custDetails = new CustomerDetails();
Word new simply tells JavaScript to create new Object while CustomerDetails() is the constructor for that object (the one that you have built up). Now that you have created an instance of your object you can use it's properties and methods like with any other object in JavaScript (String, Array, Number...). So let's do that
var custDetails = new CustomerDetails();
custDetails.setCustName("name");
custDetails.setCustSur("surname");
document.write("Your name is " + custDetails.getCustName() + " and your surname is " + custDetails.getCustSur())
The output will looke like this:
Your name is name and your surname is surname.
Hope this one will help you out.