facebook@jsbanktwitter@js_bankgoogle+javascriptbanknewsletterrss@jsbank
Guest, register






ieoBIN.com - The Best IEO List to Discover, Invest in 2019

OOP in JavaScript: Some Basics Object Oriented Programming (OOP) - a trend of programming/coding is being more and more popular cause of its benefits. And JavaScript, a most popular web programming language, also supports this trend; but in this free JavaScript article, the author just presents some basics about OOP for beginners. Please go to the full post-page for details.


For all those that use Object Oriented Programming know the benefits.  The ability to write reusable code is a real time saver in the long run.  This post will cover how to write the basic structure of Object Oriented Programming in JavaScript.

 

The Basics

For those of you that don't know what Object Oriented Programming (oop) is, it's simply a way of writing code that allows you to reuse the same code in several other projects.  Everything is encapsulated into nice little packages.  This post won't go over the actual theory of OOP, but I would definitely recommend looking into it.  I'm sure if you Googled it, thousands of entries would come up.

 

The Ways

There are a couple of ways to handle creating a class in JavaScript.

  • Placing everything inside a function
  • Placing everything inside an object
  • Using prototype to build a class

Placing Everything Inside a Function

In my opinion this is probably the easiest to read out of all of them.  However it isn't the most efficient from a processor point of view. 

Say were going to build a Dog class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Dog () {
    this.name = "";
    this.position = 0;
    this.bark = function (){
        alert("wuff");
    };
    this.walk = function(){
        this.position += 1;
        alert("position = "+this.position);
    }
    this.getName = function (){
        return this.name;
    }
}

var dog = new Dog();
dog.name = "Ralph";
dog.bark(); //Popup box with "wuff"
dog.walk(); //Position increases by 1;
alert(dog.getName()); //Outputs dog's name*/

Class methods are simply variables assigned to functions, and public variables simply use "this" infront of their names. If you wish to make a private variable, you would just create a regular variable
using "var" (e.g. var test = 1;)

 

Placing Everything Inside an Object

This is handy if you don't want to instantiate the class every time you want to use it.  The downside is that it makes inheritance a little more difficult.

Here is the same Dog class but using an Object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var Dog = {
    name    :   "",
    position:   0,
    bark    :   function(){ alert("wuff") },
    walk    :   function(){
        this.position += 1;
        alert("position = "+this.position);
    },
    getName :   function(){
        return this.name
    }
}


Dog.name = "Ralph";
Dog.bark(); //Popup box with "wuff"
Dog.walk(); //Position increases by 1;
alert(Dog.getName()); //Outputs dog's name

This method uses JavaScript Object Notation (JSON) to layout the class. Keys go on the left, and values go on the right. Once again methods are simply functions assigned to variables.

 

Using prototype to Build a Class

This is probably the preferred method of OOP in JavaScript.  It's a little harder to read, but say if we were to create thousands of dog objects, this would out perform the other methods.  Using prototypes it doesn't store the functions within the class. This means you aren't creating thousands of functions, you're only create one function that is pointed to a thousand times.  Plus it's easier to deal with inheritance.

Here is the Dog class yet again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Dog (){}
Dog.prototype.name = "";
Dog.prototype.position = 0;

Dog.prototype.bark = function (){
    alert("wuff");
}
Dog.prototype.walk = function (){
    this.position += 1;
    alert("position = "+this.position);
}
Dog.prototype.getName = function(){
    return this.name;
}

var dog = new Dog();
dog.name = "Ralph";
dog.bark(); //Popup box with "wuff"
dog.walk(); //Position increases by 1;
alert(dog.getName()); //Outputs dog's name

What the prototype keyword is doing here is creating a framework. So when the class is instanciated it will have all the methods and properties that it needs.

 

Here is a Puppy class to show how inheritance is done.  It inherits all the methods of the Dog class, but overwrites the bark method for a more puppy like bark.

1
2
3
4
5
6
7
8
9
10
11
12
13
function Puppy (){}

Puppy.prototype = new Dog();

Puppy.prototype.bark = function (){
    alert("Yelp");
}

var puppy = new Puppy();
puppy.name = "Ralph";
puppy.bark(); //Popup box with "Yelp"
puppy.walk(); //Position increases by 1;
alert(puppy.getName()); //Outputs puppy's name

 

Conclusion

This is by no means a complete tutorial of everything OOP.  This shows the basic structures of how JavaScript can handle OOP.  Out of the three that were outlined, it is probably best to stick with the prototype method. If there are any Actionscript 2 people out there, you'll feel right at home with this method.

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web