Platinum Partner
java,html5,javascript

JavaScript for Java Developers Part 1: Lack of Classes, Instatiating Objects

I admit that Javascript has never been my favorite language either, but I do admit that it is a powerful and ever more useful language to know and understand.

I have been working mostly with Java in my 9 years of development experience. In those years I have worked with many other Java developers and one of the things that many of them have in common is that they don’t like (sometimes even hate) JavaScript.

I admit that Javascript has never been my favorite language either, but I do admit that it is a powerful and ever more useful language to know and understand.

This quick tutorial will try to explain some of the points I think make Java developers feel that they hate JavaScript.

  1. Lack of Classes & Instantiating Objects
  2. Inheritance and Encapsulation
  3. Package and organizing code
  4. Functional programming and callback functions

In this first Post I will talk about point number 1.

Lack of Classes & Instantiating Objects

In Java everything we program exists within a class--even the simplest of programs need a class. Classes are used more than anything else as a blueprint for the instantiation of objects. The following code shows a simple class in java:

class Simple {
  private String propertyA;
  private String propertyB;

  public Simple(String a, String b){
    this.propertyA = a;
    this.propertyB = b;
  }

  public String concatenateAandB(){
    return propertyA + propertyB;
  }
}


In the previous chunk of code we have created a very simple Java class. This class has one explicit constructor, a couple of properties, and a public method. In order to use it we would do something like:

Simple instance = new Simple("hello ", "world");
System.out.println(instance.concatenateAandB());


That would print hello world to the console. So, how would we do something as simple as that with JavaScript? First of all let's remember that there is no such thing as classes in JavaScript. However, the previous code is easily reproducible in JavaScript. I'll show the code and then explain how it works:

function Simple(a, b) {
  var propertyA = a;
  var propertyB = b;

  this.concatenateAandB = function() {
    return propertyA + propertyB;
  }
}


The way to execute this is very similar to the Java version. We create an instance and call the method:

var instance = new Simple("hello", "world");
console.log(instance.concatenateAandB());


As you can see, the functionality here is very similar--the main difference is that, instead of having a class and a constructor for that class, we have an isolated function that, by convention, starts with a capital letter when we want it to work as a constructor. There is nothing apart from this convention that stops us from doing function simple(a, b) {.

Simple is a standard JavaScript function. However, as you can see, we called it in a non-conventional way using the new keyword. You could call this function like Simple("hello", "world") as the following chunk of code shows:

Simple("hello ", "world");
console.log(window.concatenateAandB());  


In this second scenario we can see that we are not creating a new instance with new, instead we are calling the function directly as any other JavaScript function. This has the effect of calling the function using the current object as the this object (in this case the window object). When using the new keyword, instead of using the current object as this, a new empty object is created and that one is used as the context in the function. Also, when using new, the created instance object is returned by the method call.

This is one of the most important things to know when working with objects in JavaScript. The functions behave differently when called using new or when not using new. In the first case a new empty object ({}) is created and used as the this context inside the function. When the function is called without the new keyword, the current this object (the window if you are calling from the top level) is used as the context inside the function and no new instance is created.

This makes JavaScript a bit confusing when creating objects and not having any real difference between a standard function and a constructor function. Also it is strange that constructor functions can live all on their own while in Java they are logically living inside class definitions.

In the next post I will explore how JavaScript deals with inheritance and encapsulation, and compare it to Java as I just did here.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}