Friday, March 14, 2014

Does JavaScript have classes and methods?

You sometimes hear that JavaScript is not an Object-Oriented language:  it is Prototype-Based. Which means any object can be used as a prototype for further objects. Therefore, there are no "classes", you say.  If you want "classes" you must create your own, or use some existing O-O framework built on top of JavaScript.  In the following I will argue this is not the case, really.

So what is a "class" in languages like Java, Smalltalk, or C#,  considered O-O languages? A class is something that does two things:

  1. It can create new objects, called its "instances"
  2. It determines the common properties its instances will have.

So do we have something that does those two things in JavaScript, with no libraries added? Yes we do:  The "constructor".  When we call:

 var myBook = new MyBook("Title of my Book");

we are creating a new 'instance' of MyBook.

It is  the constructor 'MyBook' that creates the instance. When it does that it assigns some common properties, or even "methods" to the instances it creates.  It might be written like this:

function MyBook(bookTitle)
{ this.titleVAR = bookTitle;
  this.title = function ()
  { return this.titleVAR; 
  return this;

So from the above we see that constructors both create new instances, and determine their common properties. Therefore we say:
  •   Constructors are the Classes of JavaScript  

But what is a "constructor" really?  Any function in JavaScript can be used as a constructor by simply prefixing a call to it with 'new'.  From this we get the perhaps more interesting result that:
  •   Functions      are the Classes of JavaScript 

When you think about it it makes sense. To create a new instance and give it some properties you need some executable code. What is the construct in JavaScript for creating units of executable code? Of course, Functions.

From this perspective JavaScript starts to look quite Object-Oriented. What about the final defining feature of O-O, objects having 'methods' ?  It is easy to see from the example above that the constructor 'MyBook' creates a new object which will have the property 'title' whose value is a method. So in JavaScript:
  •  Properties whose value is a Function are 'methods' 

There is one more feature of JavaScript that makes its objects "Objects" instead of just data-structures whose values can be functions.  That is the special behavior of the keyword  'this'.

You can see from the code-excerpt above what happens when you call myBook.title(). It executes:
   return this.titleVAR;

When this happens the pseudo-variable this is bound to the object stored in the variable myBook. The call will return the value of the property titleVAR of that specific MyBook -instance.  So we are not simply calling a function named 'title'.  We are calling the method 'title'  of a specific MyBook -instance.

We said above "Functions are classes". We can now see that all of the following are true in JavaScript:
  1. Functions are not methods. 
  2. Functions implement methods.   
  3. Functions are the classes.
  4. A Method is a property whose value is a Function

We have 'classes', and we have 'methods', which are something more than Functions. This makes me say: "JavaScript is an Object-Oriented language". It is prototype-based dynamically typed Object-Oriented language with functional programming features.

I find this interesting because when I first started working with JavaScript, coming from Smalltalk background, my immediate reaction was "Functions look like methods. Therefore they must be methods".  But really, functions are the classes  in JavaScript!  Which I think is the novel contribution of JavaScript to Object-Orientation.

© 2014 Panu Viljamaa. All rights reserved    

No comments:

Post a Comment