DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. JavaScript for Java Developers Part 1: Lack of Classes, Instatiating Objects

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.

Carlo Scarioni user avatar by
Carlo Scarioni
·
Aug. 01, 13 · Tutorial
Like (0)
Save
Tweet
Share
27.77K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Object (computer science) JavaScript Java (programming language) dev

Published at DZone with permission of Carlo Scarioni, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Host Hack Attempt Detection Using ELK
  • Benefits and Challenges of Multi-Cloud Integration
  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch
  • Demystifying the Infrastructure as Code Landscape

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: