If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.

JavaScript for Dummies?

Programming languages are like football teams - it doesn't matter
which you like, you will support it and protect its interest no matter
what. Back in the days when i was young, I remember the same old
discussion between two development teams where i used to work - C++ vs.
Delphi. I guess its an endless discussion where the people and the
languages change but the content basically stay the same.
What's weird is that when i talk to fellow developers about JavaScript
i get a weird feeling that everybody looks at it as more of a toy
rather than a powerful client side development tool. The major critic
that i get is this is a very simple functional language, that cannot
match to the object oriented concept that drive most languages. One
friend even told me that this is a language for bored teenagers.
I agree - JavaScript is an easy to learn language, but that doesn't
mean that it's a simple one. I see it as a positive thing that you can
quickly achieve your goals without needing to know all the language
capabilities, and you can learn very fast how to really use the
language capabilities to accomplish things even better or faster.
I decided to show how you can shape JavaScript to be used as an Object
Oriented language. I don't think that you must code JavaScript always
in object oriented style, but you should have this ability and choose
when you think it's the best solution to implement.

In this post i will focus on the basics of Object Oriented programming - public, private and static declarations.

Defining a Class

In Javascript everything is an object, even functions. In order to
define a new class we should just define a new function like so:

This line of code will create a new instance of the function object
and return the reference to it. Not to confuse, but if the function
return a value it won't be placed in player1. for example, let's say you have a function like so:

The difference between the two is that a will be assigned with 100 while b will be assigned with the reference to a new f1 function object.

Public Methods

As in any object oriented language, the first thing you would like
to do is define some public methods for your class. In Javascritp we
will use the prototype keyword to setup new methods for this class. You can do this like so:

These two declarations achieve the same result. This is part of the
reasons that i love javascript so much - it can be so simple and so
complicated at the same time.
Now you can use the class methods as so:

Private Variables

Now the variable m_url is not accessible to anyone. Well that
doesn't make sense, right? We want to allow our public methods to
access the data.
We solve this using closure. A closure is a protected variable space,
created by using nested functions. In order to better understand it we
first need to understand how JavaScript function declaration works.
Function declaration in Javascritp is based on two main concepts:
lexically scoped and function-level scope. Lexically means that
function run in the scope they are defined in and not in the scope they
are executed in, as other language usually do. Function-level means,
like most languages, that a variable declared in function is not
accessible outside that function. Using these concepts we can now
declare our private variables that will be accessible to our public
methods:

The difference between this code and the previous ones, is that the public methods were declared using the prototype while now they are declared using the this keyword. The main difference is in the way the JavaScript engine execute this code. When declaring functions using the prototype, the Javascript engine creates only one instance of the function in memory and dynamically assigns the this according
to the calling object. On the other hand, when declaring a method
inside the object constructor, as we did in the last example, the
JavaScript engine will create a copy of that function in memory for
every instance that you create from the Player class. Usually this
shouldn't be a big factor, but its important to know the difference.

Static Methods & Variables

Using closures and the prototype characteristic i mentioned above you can also create static methods like so: