HTML5 Zone is brought to you in partnership with:

As a pasionate software developer, motivated by learning and appliyng innovative and interesting software development tools, techniques and methodologies, my professional objectives are the following. To be in a technology oriented enterprise where the technichal staff is the soul of the company. To be in an important IT team. Be able to design and develop state of the art software. Be able to apply new knowledge everyday, on innovative ways and with a great degree of freedom. To architect, design and develop software that uses the best practices of the field. Play with the latest technologies, learn everyday and participate in the research and innovation of the software products. Carlo is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

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

08.01.2013
| 14049 views |
  • submit to reddit

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 Carlo Scarioni, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Gergo Nagy replied on Thu, 2013/08/01 - 2:34pm

Yes this is a very important different between Java and JavaScript that Java programmers have to memorize. If you keep this in mind you have solved almost 80% of misunderstandings.

In Java this means:

"Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called."

see also: Oracle Java tutorial

While in JavaScript this means:

"In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of."

see also: quirksmode.org 

Good start of the series!

Keep writing! ;)

Diego Bravo replied on Tue, 2013/10/08 - 12:52pm

 Hi, when we can read the part two?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.