Explanation of JavaScript from the perspective of Java developers

Source: Internet
Author: User
We cannot explain all the details of JavaScript in a blog. If you are more or less involved in web application development, our Java tools and technical scope reports reveal that most (71%) Java developers are classified into this category, but you have encountered obstacles to JavaScript. We cannot explain all the details of JavaScript in a blog. If you are more or less involved in web application development, our Java tools and technical scope reports reveal that most (71%) Java developers are classified into this category, but you have encountered obstacles to JavaScript.

There is no doubt that you already know Java and JavaScript. No matter how similar names they are, they do not share much in common. Java's static type, simple syntax that conforms to the direct Law, and lengthy are very different from JavaScript's dynamic, lack of consistency principles, and weird.

However, JavaScript is a web programming language. Recently, due to the development of Node. js and JVM Nashorn JavaScript Engines, it has gained considerable attention on the server side.

In this article, I don't want to talk about the good or bad JavaScript, or repeat the countless JavaScript Tutorials that anyone can find for free. I would like to list some technical points that help to understand JavaScript as a language and understand it from a perspective close to horse.

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

Versatility of JavaScript

Functional Programming in JavaScript

Different from Java inheritance

In addition, you will find some tool recommendations. Without these tools, you do not want to start JavaScript projects, including the code quality analysis and testing framework tools for building the system.

Advantages

Write it once and run it almost everywhere!

Without doubt, JavaScript is a web programming language. It is the compilation goal of many other languages and the ultimate way to prove that sometimes people just want to have more free time. However, this is not a bad thing. Every computer that can browse modern websites is equipped with a performance and available JavaScript Engine. Most importantly, JavaScript code can be run on the backend.

Built-in Nashorn for our favorite JVM, lightweight, high-performance JavaScript runtime, fully capable of interpreting JavaScript scripts, as well as the JavaScript scripts with Java code in the project.

JavaScript is a perfect continuation of the Java experience, given the freedom that every computer can gain when running.

Function programming: first-class citizens are functions rather than 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 functional programming world, which is the perfect method for structured JavaScript programming.

Note: Objects in JavaScript are ing everything. Each attribute of an object is in the same ing: function, property, and constructor; variability brings greater risks. For Java, you can at least ensure that the methods and field structures are stable to some extent.

This, in turn, makes functional programming more advantageous: it involves small, understandable functions and unchanged data structures that run in JavaScript.

This is not without justification. The following is an example of defining a reduce function in JavaScript, from 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 JavaScriptreturn a + b;}  function sum(numbers) {return reduce(add, 0, numbers);}

Note: The recursive version of reduce is not used here. JavaScript is not characterized by the end call [NOTE 1], which means that the recursive version of each function will use the depth of the stack. Like Java, if you recursion is too deep, the program will crash.

Inheritance: like the real world

JavaScript inheritance is prototype-based. That is, you have not extended other types. In fact, your instances inherit the functions from other instances.

Imagine that object A is like A ing. We just mentioned A little, but different perspectives are used. Then another ing-like object B inherits everything from object.

This shows that B can access all parts of A: A's method, fields, and so on.

In practice, I have never seen anyone actually use simple prototype-based inheritance. Generally, when someone needs to inherit, he only constructs classes, so you can use all the extensive skills and class-based inheritance work modes.
-- Rene Saarsoo, XRebel front-end engineer

I'm not sure what Java developers should learn from it, but be careful about the different inheritance methods. I should pay special attention to parent-level objects, rather than Accidentally changing the behavior of the entire program.

Avoid at any time

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

Note: In JavaScript, whenever the var keyword is not used to define variables, the Defined variables are pushed to the top of their defined scopes. This means that every variable defined in this way will go to the top of the global scope, which will lead to conflicts and unexpected headaches between you and your colleagues.

You can enable strict mode. You only need to write "use strict" on the top of the script file, so the casual global variable declaration will display an error.

Another important difference between JavaScript and Java is that the former is a dynamic type language, which means that everything can be of any type. This is obvious and cannot be emphasized anymore: Do not reuse the same variable for different types of values.

Tracking is a string-type variable at the beginning, but now it is a floating point or function. Believe me!

Also, I don't want to go too deeply into the discussion of types and boolean values, but be cautious with the implicit type conversion thrown to you by the JavaScript engine.

Tips

As I mentioned above, you should pay more attention to the syntax and quirks of this language in programming, instead of simply knowing it. Projects rarely fail due to lack of language, and more failures are related to the lack of overall project framework. The following are some tools that help you deliver projects.

Static code analysis

Most projects are different, and their complexity and needs lead to a lot of details. How do you proceed with the code library. Even so, there is a consistency goal in all places, that is, the code quality.

Yes, code quality. The most important task for any developer is delivery. But don't compromise on quality, don't feel unconfident about the code you submit, And you're reluctant to share it with your colleagues.

Fortunately, JavaScript has a decent solution-JSHint. JSHint is a static analysis tool tailored for JavaScript, similar to FindBug applied to Java code. JSHint can run in your code library and highlight suspicious or problematic areas. Even if you do not produce bugs immediately, these areas will become difficult to maintain in the future. It is quite simple to support it in projects. Help yourself-if you are writing JavaScript code, use JSHint to make it safer and less embarrassing.

REPL

REPL stands for the Read-Eval-Print Loop [NOTE 2], which is a powerful tool in many dynamic languages. If you have read Scala or Groovy, you must be able to understand this concept.

One way to activate JavaScript REPL is to open the browser console, which produces an interface to evaluate JavaScript code.

In Jasmine, you describe the test suite with describe, which blocks access to the code you want to test. After the code in the test is complete, you have some CT 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 applied ZeroTurnaround projects in product development in private, especially XRebel, an interactive analyzer that is rich in JavaScript and runs continuously.

Build tools

Finally, what you need for your project is the building tool. If you use JavaScript in a Java project, make sure that you can avoid the Java build tool, which is almost enough. However, for independent JavaScript projects, there is no need to introduce the monster-Maven [NOTE 3 ].

The building tool used by the JavaScript project that can be considered is GulpJS [Note 4 ]. It is a plug-in-based build system for which you can specify tasks. The task can be "copying. js files under the src directory to dest", or "compressing my JavaScript code for the production environment ". It is shocking that GulpJS adds the file stream related to the task to the filter, so you can add the preceding two tasks to an effective cleaning.

There are also a large number of available plug-ins. With proper system construction, you will find collaboration in projects much easier.

Conclusion

We only 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 preparing to go into JavaScript projects without hesitation, this will help you get started, embracing JavaScript will help you not get frustrated frequently.

Do you know the secret or best practices that keep JavaScript developers happy? There is no doubt we should share it! Comment below or talk to me on Twitter: @ shelajev. I am happy to hear your thoughts!

Note 1: in computer science, tail call refers to the case where the last action in a function is a function call: that is, the return value of this call is directly returned by the current function. In this case, the call position is the end position. If this function is called at the end (or another function called at the end), it is called tail recursion, which is a special case of recursion. Tail call is not necessarily a recursive call, but tail recursion is particularly useful and easy to implement. Http://zh.wikipedia.org/wiki/end call

NOTE 2: REPL is a simple and interactive programming environment. This term is often used to refer to a Lisp interactive development environment, but it can also refer to command line modes and such as APL, BASIC, Clojure, F #, Haskell, J, Julia, Perl, PHP, similar programming environments in programming languages such as Prolog, Python, R, Ruby, Scala, Smalltalk, Standard ML, Tcl, and Javascript. This is also called an interactive toplevel component ). Http://zh.wikipedia.org/wiki/%E8%AF%BB%E5%8F%96%EF%B9%A3%E6%B1%82%E5%80%BC%EF%B9%A3%E8%BE%93%E5%87%BA%E5%BE%AA%E7%8E%AF

NOTE 3: In addition to program building capabilities, Maven also provides Ant's lack of advanced project management tools. Because Maven's default build rules are highly reusable, it is often possible to build a simple project using two or three lines of Maven build scripts, while Ant requires more than a dozen lines. In fact, due to Maven's project-oriented approach, many Apache Jakarta projects now use Maven, and the proportion of Maven used by corporate projects continues to grow. Http://www.oschina.net/p/maven

Note 4: Writing HTML, CSS, and Javascript from the beginning is a task of the last century. Today, JavaScript is written in an editor that supports abbreviations such as CoffeeScript. If you want to write JavaScript to complete code cleaning and optimization, Gulp is your best choice. GulpJS is similar to Ant or Maven in Java. Http://www.oschina.net/p/gulp

Original article: javascript-explain-it-like-im-a-java-developer Translation: labazhou

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.