Explaining JavaScript from a Java developer's perspective

Source: Internet
Author: User

We can't explain all the details of JavaScript in a it. If you're more or less involved in Web application development, our Java tools and technology coverage report reveals that most (71%) Java developers are grouped in this category, but you're stuck with JavaScript.

Without a doubt, you already know Java and JavaScript, no matter how similar they are named, and don't share much in common with each other. The static type of Java, the simple syntax and the verbosity that conform to the direct rules, are very different from the dynamics of JavaScript, the lack of consistency and the strangeness.

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

In this article, I don't want to just ramble on about JavaScript's good and bad, or repeat the countless JavaScript tutorials that anyone can find for free. I would like to list some of the technical points that will help you understand JavaScript as a language and understand it from a point of view that is close to horse.

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

    • The versatility of JavaScript
    • JavaScript's function programming problem
    • Inheritance different from Java

In addition, you will find some tools to recommend, without these tools, you do not want to embark on the JavaScript project, including the building system of code quality analysis and testing framework tools.

The pros are written once and run almost everywhere!

JavaScript is undoubtedly a web programming language, a compilation target for many other languages, and the ultimate way to prove that sometimes people just want to have more free time. Still, this is not a bad thing. Every computer that can browse a modern web site is equipped with a performance-and-available JavaScript engine. Most importantly, JavaScript code can run on the back end.

The lightweight, high-performance JavaScript runtime Nashorn, built into our favorite JVM, is fully capable of interpreting JavaScript scripts and interpreting JavaScript scripts with Java code in the project.

JavaScript is a perfect continuation of the Java experience, given the freedom that every computer runs.

Functional programming: A one-class citizen is a function, not a recursive

Functions in JavaScript are the first class of citizens, and they are values that can be stored in variables, passed to other functions, and executed at the appropriate time.

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

Note that the object in JavaScript is a mapping of anything, and each attribute of the object (attribute) is in the same map: The function, the property, the constructor, and the variability poses a greater risk, and for Java, You can at least ensure that the method and the field structure are somewhat stable.

This, in turn, makes functional programming more advantageous: It involves small, understandable functions, and unchanging data structures that are the way they run in JavaScript.

This is not without basis, the following is an example of defining a reduce function in JavaScript, from the book "Eloquent JavaScript".

1234567891011121314151617181920 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: We do not use the recursive version of reduce here. JavaScript does not feature a tail call of "Note 1", which means that the recursive version of each function will use the depth of the stack, as in Java, and if you are too deeply recursive, the program crashes.

Inheritance: Just like the real world

The inheritance of JavaScript is based on prototypes. That is, you have not extended other types of types, and in fact, you have instances that inherit functionality from other instances.

Imagine that object A is like a map, we just mentioned a little bit, but with a different perspective, and then another map-like object B inherits everything from A.

This means that B can access all parts of a: A method, a field, and so on.

In practice, I never see anyone actually using simple prototype-based inheritance. Usually when someone needs to inherit, he just constructs the class, so you can use all the broad skills, and class-based inheritance of the working pattern.
--rene Saarsoo,xrebel Front-end engineer

I'm not sure what Java developers should learn from them, but beware of the differences in how they inherit, and pay extra attention to the parent object rather than accidentally altering the entire program's behavior.

To avoid at any time

The decision to list unreliable JavaScript designs is easier than imagined. The most obvious place to avoid in a JavaScript program is the declaration of a global variable.

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 scopes. This means that each variable defined in this way will run to the top of the global scope, which can cause conflicts and unexpected headaches for you and your coworkers.

You can turn on strict mode. Simply write "Use strict" at the top of the script file, and the inadvertently written global variable declaration will show an error.

Another important difference between JavaScript and Java is that the former is a dynamic type language, and the truth is that everything can be of any type. This is obvious and cannot be overemphasized: do not reuse the same variables for different types of values.

The trace was initially a string-type variable, but now it's a floating-point number, or a function, trust me!

Also, I don't want to go too deep into the discussion of type and Boolean values, but be wary of the implicit type conversions that the JavaScript engine throws at you.

Tips to get the job done

As I mentioned above, it is more important to pay more attention to the grammar and quirks of this language than to know it programmatically. Projects rarely fail due to lack of language, and more failures are related to inadequate overall project frameworks. Here are some tools to help you deliver your project.

Static code Analysis

Most projects are different, their complexity and requirements lead to a lot of detail, how do you get started with the code base? Nonetheless, there is a consistent goal everywhere, and that is the quality of the code.

Yes, code quality, the most important job for any developer is delivery. But don't compromise on quality, don't feel insecure about the code you're submitting, and be reluctant to share it with coworkers.

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

Repl

The REPL represents a "read-evaluate-output" loop (Read-eval-print loop) "Note 2", which is a powerful tool for many dynamic languages. If you've seen Scala or groovy, you'll be able to understand the concept.

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

Another handy tool is JJS, which is bundled in 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 stringent JavaScript scripts.

Test

For any project, you want to run some tests. Testing is especially important for dynamic types of languages, and it is best to choose a test framework. I recommend Jasmine, which is a behavior-driven development framework for testing JavaScript.

In Jasmine, you describe the test suite with describe, which blocks the code access you want to test. After the code in the test is complete, you expect some results.

Obviously this is not a tutorial, but I want to give you a glimpse of how elegant the JavaScript code looks. Jasmine is one of the best practices for JavaScript projects, and we have privately applied it to zeroturnaround projects in product development, especially for JavaScript-rich, uninterrupted-running interactive analyzers Xrebel.

Build tools

Finally, your project will need, and more importantly, build tools. If you use JavaScript in a Java project, make sure that you can avoid the Java build tool, which is almost enough. However, for standalone JavaScript projects, it is not necessary to introduce Leviathan-maven "NOTE 3".

The build tool that can be considered for JavaScript projects is Gulpjs "Note 4". It is a plug-in-based build system that you can assign tasks to. The task can be "copy. js files from src directory to dest", or "compress my JavaScript code for production environments". What's shocking is that Gulpjs adds the task-related file stream to the filter, so you can add the two tasks above to a valid sweep.

There are plenty of plug-ins available, and with the right build system, you'll find collaboration in your project much easier.

Conclusion

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

Do you know the secrets or best practices that make JS developers happy? There is no doubt to share! Talk to me in the comments below or on Twitter: @shelajev. I'm happy to hear your thoughts!

  • Note 1: In computer science, a tail call refers to the case where the last action in a function is a function call: that is, the return value of the call is directly returned by the current function. In this case, the call position is the trailing position. If the function calls itself at the tail position (or another function of a tail call itself, etc.), it is called a tail recursion, which is a special case of recursion. Tail calls are not necessarily recursive calls, but tail recursion is particularly useful and easier to implement. http://zh.wikipedia.org/wiki/Tail Call
  • Note 2:repl is a simple, interactive programming environment. This word is often used to refer to a Lisp interactive development environment, but it can also refer to the command line mode and for example APL, BASIC, Clojure, F #, Haskell, J, Julia, Perl, PHP, Prolog, Python, R, Ruby, Sca Such programming languages as LA, Smalltalk, Standard ML, TCL, and Javascript have a similar programming environment. This is also called an interactive top-level component (interactive toplevel). 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:maven provides advanced project management tools that are missing from Ant, in addition to the ability to build programs. Because MAVEN's default build rules are highly reusable, it is often possible to build a simple project with two or three rows of maven building scripts, while using ANT requires more than 10 rows. In fact, because of MAVEN's project-oriented approach, many Apache Jakarta projects now use MAVEN, and the proportion of company projects using MAVEN continues to grow. Http://www.oschina.net/p/maven
  • Note 4: Writing html\css\javascript from scratch is a matter of the last century, and today's JavaScript is written in an editor that supports syntactic abbreviations such as coffeescript. If you want to finish writing JavaScript to be able to complete the Code cleanup optimization work, Gulp is your best choice, gulpjs similar to Ant or Maven to Java. Http://www.oschina.net/p/gulp

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

Explaining JavaScript from a Java developer's perspective

Related Article

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.