Explain JavaScript from the perspective of Java developers

We can't explain all the details of JavaScript in a blog post. If you are more or less involved in web application development, our java tools and technology scope report reveals that most (71%) Java developers fall into this category, but you are hindered by JavaScript.

There is no doubt that you already know Java and JavaScript. No matter how similar they are named, they don't share much in common with each other. Java's static type, simple syntax in line with direct laws and verbosity are very different from JavaScript's dynamic, lack of consistency principles and weird.

However, JavaScript is the programming language of the web, recently due to node JS and the development of the JVM's own nashorn JavaScript engine have received considerable attention on the server side.

In this article, I don't want to just talk about the good and bad of JavaScript, or repeat the countless JavaScript tutorials that anyone can find for free. I want to list some technical points that are helpful to understand JavaScript as a language, and understand it from the perspective of approaching horse.

We will include the following language level technical points in this article:

In addition, you will find some tool recommendations. Without these tools, you don't want to start a JavaScript project, including tools for building a system's code quality analysis and testing framework.

Advantages

Write once and run almost everywhere!

There is no doubt that JavaScript is a web programming language, the compilation target of many other languages, and the ultimate way to prove that sometimes people just want to have more free time. Nevertheless, this is not a bad thing. Every computer that can browse modern websites is equipped with a JavaScript engine with performance and availability. Most importantly, JavaScript code can run on the back end.

Nashorn, a lightweight high-performance JavaScript runtime built into our favorite JVM, can fully interpret JavaScript scripts and JavaScript scripts with java code in the project.

Given the freedom available to every computer when it runs, JavaScript is a perfect continuation of the Java experience.

Functional programming: first class citizens are functions, not recursion

Functions in JavaScript are the first type of citizens. They are values that can be stored in variables, passed to other functions, and executed when appropriate.

This opens the door to the world of functional programming, which is the perfect way of structured JavaScript programming.

Note that the object in JavaScript is a mapping of anything, and each attribute of the object is in the same mapping: function, property and constructor; variability brings greater hidden dangers, and for Java, you can at least ensure that the method and field structure are stable to some extent.

In turn, this makes functional programming more advantageous: involving small, understandable functions and invariant data structures is the way to run in JavaScript.

This is not groundless. The following is an example of defining a reduce function in JavaScript, which comes from the book eloquent JavaScript.

function forEach(array,action) {
for (var i = 0; i < array.length; i++) {
action(array[i]); //apply action to every element of the arra.
}
}
 
function reduce(combine,base,array) {
forEach(array,function (element) {
base = combine(base,element); // and here we apply function passed as ‘combine’ parameter to ‘base’ and ‘element’
});
return base;
}
 
function add(a,b) { // btw,this is how you define a function in JavaScript
return a + b;
}
 
function sum(numbers) {
return reduce(add,numbers);
}

Note: we do not use the recursive version of reduce here. JavaScript does not feature tail calls, which means that the recursive version of each function will use the depth of the stack. Like Java, if you recurse too deeply, the program will crash.

Inheritance: like the real world

JavaScript inheritance is prototype based. That is, you do not extend types of other types, but in fact, the instances you own inherit functions from other instances.

Imagine that object a is like a mapping. We just mentioned some, but from a different perspective, and then another mapping like object B inherits everything from a.

This means that B can access all parts of a: A's methods, fields, and so on.

I'm not sure what Java developers should learn from it, but beware of different inheritance methods. Pay special attention to parent objects and don't accidentally change the behavior of the whole program.

To avoid at any time

Listing unreliable JavaScript design decisions is easier than you think. The most obvious thing to avoid in JavaScript programs is the declaration of global variables.

Note that in JavaScript, whenever you do not use the VaR keyword to define variables, the defined variables are pushed to the top of their defined scope. This means that each variable defined in this way will run to the top of the global scope, which will lead to conflict and unexpected headaches for you and your colleagues.

Strict mode can be turned on. Just write "use strict" at the top of the script file, and the inadvertently written global variable declaration will display an error.

Another important difference between JavaScript and Java is that the former is a dynamically typed language, and its essence is that everything can be of any type. This is obvious and can't be emphasized any more: don't reuse the same variables for different types of values.

Trace started as a string variable, but now it's a floating point number or function. Trust me!

Also, I don't want to go into the discussion of types and Boolean values, but be wary of the implicit type conversion thrown by the JavaScript engine.

Tips for getting the job done

As I mentioned above, we should pay more attention to the grammar and quirks of this language in programming, not just know. Projects rarely fail due to lack of language, and more failures are related to the lack of overall project framework. Here are some tools to help you deliver your project.

Static code analysis

Most projects are different, and their complexity and requirements lead to a lot of details. How do you start with the code base. Nevertheless, there is a consistent goal everywhere, that is, code quality.

Yes, code quality. For any developer, the most important job is delivery. But don't compromise on quality, and don't be reluctant to share the code you submit with your colleagues because you are not confident.

Fortunately, JavaScript has a decent solution - jshint. Jshint is a static analysis tool tailored for JavaScript, which is similar to findbug applied to Java code. Jshint can run in your code base and highlight suspicious or problematic areas. Even if you don't generate bugs immediately, these areas will become difficult to maintain in the future. Supporting it in a project is fairly simple. Do yourself a favor - if you're writing JavaScript code, use jshint to make it safer and less embarrassing.

  REPL

Repl stands for read Eval print loop [Note 2]. It is a powerful tool for many dynamic languages. If you have seen Scala or groovy, you must be able to understand this concept.

One way to activate JavaScript repl is to open the browser's console, which produces an interface for evaluating JavaScript code.

Another convenient tool is jjs, which is bundled with jdk1 8。

It is a command-line tool that allows you to access the nashorn JavaScript engine in the JDK and is fully capable of executing even the most rigorous JavaScript scripts.

Testing

For any project, you want to run some tests. Testing is particularly important for dynamically typed languages, and it is best to choose a testing framework. I recommend jasmine, a behavior driven development framework for testing JavaScript.

In jasmine, you use describe to describe the test suite, which prevents access to the code you want to test. After the code in the test is completed, you expect some results.

Obviously, this is not a tutorial, but I want to give you a glimpse of how elegant JavaScript code looks. Jasmine is one of the best practices of JavaScript projects. We have privately applied it to the zeroturnaround project in product development, especially for xrebel, an uninterrupted interaction analyzer rich in JavaScript.

Build tools

Finally, what your project will need, and more importantly, is a build tool. If you use JavaScript in Java projects, make sure you can avoid Java build tools, which is almost enough. However, for stand-alone JavaScript projects, there is no need to introduce the behemoth Maven [Note 3].

Gulpjs [Note 4] is the construction tool that can be considered for JavaScript projects. It is a plug-in based construction system, and you can specify tasks for it. The tasks can be "copy the. JS file in SRC directory to dest" or "compress my JavaScript code for production environment" 。 What makes people shocked is that gulpjs adds the task related file stream to the filter, so you can add the above two tasks to an effective cleaning.

There are also a large number of plug-ins available. With the appropriate build system, you will find that collaboration in the project will be much easier.

Conclusion

We just saw the tip of the iceberg of JavaScript and tried to introduce some concepts and tools that Java developers should know when solving JavaScript. Naturally, there is no complete list of technologies to learn, but if you are ready to go deep into JavaScript projects, it will help you get started, and embracing the quirks of JavaScript will help you not get frustrated frequently.

Do you know the secrets or best practices that make JS developers happy? There is no doubt that we should share! Comment below or talk to me on Twitter: @ shelajev. I'm glad to hear what you think!

Original text: Javascript explain it like im a java developer translation: Labazhou

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>