Javascript object in the eyes of Java developers

One of the biggest challenges for Java developers when learning and applying Javascript is the difference between different interpretations of objects between the two languages.

Looking at it, Javascript has quite a similar syntax to Java, but the Javascript object-oriented programming approach with prototype-based is extremely different from the class-based Java.

In this article, I will present Javascript object under the perspective of a Java developer, and give a few tips and strategies for Java developers to use better, as well as link an object-oriented programming concept. in Java and Javascript.

While Java and some other programming languages ​​have a variety of datatypes as well as collection types, Javascript only has a few primitive types such as Boolean, Number, String, Null and Undefined, a type similar to collection type is Array, and supports other Javascript objects. Of course this makes JavaScript learning simpler, but also means that in specific functions, we have to implement the types or collections that other languages ​​may already provide.

Now we go to the main problem, for the object, the first thing is initialization.

Initialize the JavaScript object.

There are many ways to initialize the JavaScript object. But in the eyes of a Java developer, it is common to prefer to initialize an object using a constructor. One of the particularly important advantages of using this method is that the created object can be used with many different constructors, corresponding to different purposes. On the other hand, this approach is also a close way to Java as well as the most class-based languages.

Above is how to initialize an object using a constructor, in addition there is a way to initialize another object by using the initializer object.

This method is a single-use method because no function is declared for initialization. As well as its syntax is extremely different from Java.

And when running the two above code we get the following result.

Add toString () function to Javascript object.

Why is toString ()? All objects in Java are inherited from Object class, toString is a method that every object has.

Like Java, all Javascript objects are extended from a common object called Object , and especially objects all inherit Object.prototype properties.

In this case, Object.prototype.toString () provides a string to represent all Javascript objects. In the above result, the display result is the most concise form, which is similar to the default return value of the toString () function in Java.

Just as it is possible to override the toString () function in Java so that the results return more useful things, the method to initialize the object with constructor in Javascript allows us to override Object.prototype.toString () function. Below is the specific code for that.

The results are now more useful.

From there you can see the similarity between Java and Object.prototype java.lang.Object in Javascript.

Avoid using with global scope in Javascript

Javascript makes it easy to use variables in global scope. The variables in Javascript function are limited to function scope which is declared with the keyword var. Conversely, variables declared that are not in any function are not restricted at all and therefore every change that affects that variable affects all functions using it. This idea in Javascript makes almost all Java developers want to go crazy.

This keyword is another complicated surprise from Javascript because it depends a lot on how it is used, where it is called, and whether it is in strict mode. . In other words, using this in Javascript is much harder than Java, because in Java it simply refers to a specific instance of the class. However, when the function in Javascript is used as a constructor (with the new keyword) then the this keyword will be bound by the object being created, which is why in the constructor function charge on me using this .

Javascript object is similar to Java Map.

Take a look at the following simple example of how to declare an object in Javascript.

For Java Map.

Obviously there is a similarity right.

Summary.

Although there are common syntax points, even names have four letters in common, but Java and Javascript are very different at many points. In fact, while both can be said to be object-oriented or object-based, but the behavior for object creation in each language is different, one is class-based (Java) and the other is prototype-based (Javascript).

Understanding some basic similarities as well as differences of the two languages ​​can help developers easily switch between the two languages. On the other hand, in ECMAScript 6 (ES6), they also supported classes with keyword classes, constructor, extend to create similarities, to support developers to use other object-oriented languages ​​more accessible.

See you in the next article, maybe an introduction to ES6.

ITZone via codealohicguy

Share the news now