The Pragmatic Programmer

Tulisan iseng kalo ada waktu.
See also: Other Geeks@INDC

Why JavaScript is an Object-oriented Language?

Object-oriented language is characterized by this three concepts:

1. Encapsulation
2. Inheritance
3. Polymorphism

If you are familiar with VB.Net and C# then you are most certainly know how to implement those concepts in your language.

But......What if I say that JavaScript is also an object-oriented language???

"Hahaha...!!!!". You will must certainly laugh at me, don't ya?? :)

OK! Here is my reasons:

Creating an Object
JavaScript can create objects. To create an object in JavaScript is simple:

function ObjectA(InitialValue){

this.X = InitialValue;

}

var a = new ObjectA(10);
alert(a.X); //this will display '10'

"OK! I can instantiate an object in JavaScript, but how to add a method to the object? Can JavaScript do that??"

Off course! There are two ways to do that. You can do it in the simple way or in a more longer way. Here is the simple way:

function ObjectA(InitialValue){

this.X = InitialValue;
this.SomeMethod = function(){
      alert("Message from SomeMethod()");
}

}

var a = new ObjectA(10);
a.SomeMethod(); //this will show the alert box.

And this is the more longer way:

function ObjectA(InitialValue){

this.X = InitialValue;

}

ObjectA.prototype.SomeMethod = function(){

alert("Message from SomeMethod()");

}

var a = new ObjectA(10);
a.SomeMethod(); //this will show the alert box.

"Ya, I see now that JavaScript can instantiate an object. But how about Encapsulation? Can JavaScript hide private member variable?"

Encapsulation
The JavaScript way to private member is by using closure:

function ObjectA(InitialValue){

var X = InitialValue;
this.GetX = function(){
      return X;
}

this.SetX = function(value){
      X = value;
}

}

var a = new ObjectA(10);
a.SetX(20);
alert(a.GetX()); //this will display '20'

Hey, its a getter and setter property ya? 

"Naa...Many language can create object and do some encapsulation but not many language can do inheritance. So how to do that in JavaScript?"

Inheritance
JavaScript is a prototype-based language. Not like VB.Net or C# that is a class-based language where there is a distinction between a class definition and the class instance. In JavaScript, it uses prototype. A prototype is an object that is used as a template for every new object. Confuse?? Sure!!! But the point is, to do inheritance in JavaScript is different with VB.Net and C# is. Here is why:

function Animal(AnimalName){

this.Name = AnimalName;

}
Animal.prototype.Walk = function(){

alert("The animal is walking.");

}

function Dog(DogName){

Animal.call(this, DogName);

}
Dog.prototype = new Animal();

var d = new Dog("You :)");
d.Walk();
alert(d.Name);

"What a weirdo way of to inherit?". Yeah...Its the JavaScript way! :) 

"I see! I am courius to see how to do polymorphism."

Polymorphism
Just like VB.Net and C#. JavaScript can do polymorphism and the subclass can call the overriden method in the base class. Here is how:

function Animal(AnimalName){

this.Name = AnimalName;

}
Animal.prototype.Walk = function(){

alert("The animal is walking.");

}

function Dog(DogName){

Animal.call(this, DogName);

}
Dog.prototype = new Animal();
Dog.prototype.Walk = function(){

Animal.prototype.Walk.call(this);
alert("The dog is walking");

}

var d = new Dog("You :)");
d.Walk();
alert(d.Name);

Conclusion
Because JavaScript can do all of OOP concepts then JavaScript is an object-oriented language. Do you agree with me?

See ya!!

Share this post: | | | |

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: