Chapter 9. Tidying up Your Code Using OOP

In this chapter, we are going to learn about object-oriented programming (OOP) and discuss the code of the famous game, Hangman.

"OOP is a programming paradigm that uses abstraction to create models based on the real world. OOP uses several techniques from previously established paradigms, including modularity, polymorphism, and encapsulation." or "OOP languages typically are identified through their use of classes to create multiple objects that have the same properties and methods."

You probably have assumed that JavaScript is an object-oriented programming language. Yes, you are absolutely correct. Let's see why it is an OOP language. If a computer programming language has the following few features, we call it object oriented:

  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

Before going any further, let's discuss objects. We create objects in JavaScript in the following manner:

var person = new Object();
person.name = "Harry Potter";
person.age = 22;
person.job = "Magician";

We created an object for a person. We added few properties of person.

If we want to access any of the property of the object, we need to call the property.

Consider that you want to have a popup of the name property of the preceding person object. You can do this with the following method:

person.callName = function(){
  alert(this.name);
};

We can write the preceding code as shown in the following:

var person = {
  name: "Harry Potter",
  age: 22,
  job: "Magician",
  callName: function(){
  alert(this.name);
  }
};

Inheritance in JavaScript

To inherit means derive something (such as, characteristics, quality, and so on) from one's parents or ancestors. In programming languages, when a class or object is based on another class or object in order to maintain the same behavior of the parent class or object is known as inheritance.

We can also say that this is a concept of gaining properties or behaviors of something else.

Suppose, X inherits something from Y; it is like X is a type of Y.

JavaScript occupies the inheritance capability. Let's take a look at an example. A bird inherits from an animal as a bird is a type of animal. Therefore, a bird can do the same thing that an animal does.

This kind of relationship in JavaScript is a little complex and needs a syntax. We need to use a special object called prototype, which assigns the properties to a type. We need to remember that only function has prototypes. Our Animal function should look similar to the following:

function Animal(){
//We can code here. 
}; 

To add a few properties of the function, we need to add a prototype as shown in the following:

Animal.prototype.eat = function(){
  alert("Animal can eat.");
};

Let's create prototypes for our Bird function. Our function and prototypes should look similar to the following:

function Bird(){
};
Bird.prototype = new Animal();
Bird.prototype.fly = function(){
  alert("Birds can fly.");
};
Bird.prototype.sing = function(){
  alert("Bird can sing.");
};

The result of the prototypes and function is that any Bird that you create will have the properties of Animal and Bird. However, if you create Animal, this will only have the properties of Animal. The properties of Animal are inherited by Bird.

Therefore, we can say that JavaScript has inheritance property.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.244