The dojo concept for Java Developers

Source: Internet
Author: User
Tags hash integer

Dojo is becoming more and more popular in web-based applications. Many developers are expert in Java™ programming, but lack experience in JavaScript. Moving from a strongly typed, object-oriented compiler language to a dynamic, weakly typed scripting language, developers need to experience the difficulty of conceptual transitions. This confusion makes it difficult for developers to declare Dojo classes correctly. This article will help tease out this confusion, explain why the context must be set, and how to implement it.

Brief introduction

If you are a developer with little or no JavaScript experience, you may need to master some of the necessary concepts when contacting Dojo. One of the main problems with Dojo (at the time of this article) is that it is still in its infancy (version 1.0 was released in February 2008), and the available documentation is still very limited. This article will help you understand the connection between Dojo and Java code, allowing you to quickly start and master the Toolbox as you develop your application.

This article does not describe how to obtain the Dojo Toolbox or some of the necessary usage instructions because there is already a large amount of resources available for such information. This article focuses on Web developers who have turned to Dojo from servlet development.

JavaScript Hash

One of the major challenges to be faced is to understand the syntax used when invoking Dojo functions, especially "hash" or JavaScript objects. The hash is represented as a set of properties that use a comma interval, and is enclosed in braces. Listing 1 shows a simple example that declares a hash that contains 6 attributes: A string, an integer, a Boolean value, an undefined attribute, another hash, and a function.

Listing 1. Sample JavaScript Hashvar myHash = {
  str_attr : "foo",
  int_attr : 7,
  bool_attr : true,
  undefined_attr : null,
  hash_attr : {},
  func_attr : function() {}
};

Note that JavaScript is weakly typed, so although each property is initialized to a value associated with its name, it is still necessary to set the Str_attr property to an integer or Boolean value (or any other type). Use the dot operator to access or set each property in the hash (see Listing 2).

Listing 2. Accessing and setting the hash property// Accessing a hash attribute...
console.log(myHash.str_attr);
// Setting a hash attribute...
myHash.str_attr = "bar";

The meaning of the first four attributes of Myhash is self-evident. In fact, hash can have a hash attribute, which is not surprising. (This can be seen as a Java class similar to primitives and objects). This is the last important attribute that needs to be understood.

Functions and objects

Although there is a Java.reflection.Method class in the Java code, it actually serves only as a wrapper for the method. In JavaScript, this function can be any object that can be set, referenced, and passed as a parameter to another function. In general, as with declaring an anonymous inner class in a Java method call, you also need to declare a new function in a function call.

Another important difference between Java methods and JavaScript functions is that JavaScript functions can run in different contexts. In Java programming, use the This keyword to refer to the current instance of the class that you are using. When used in a JavaScript function, this refers to the context in which the function runs. If not specified, the function will run in the closure that defines it.

In the simplest case, closures can be thought of as arbitrary JavaScript code contained in braces ({}). A function declared inside a JavaScript file can use this to access any variable declared in the body of a file, but a function declared in a hash can only use this to refer to a variable declared within the hash, unless the other context is provided.

Because it is often necessary to use closed functions as arguments to Dojo functions, understanding how to set the context eliminates a lot of debugging work.

The main Dojo function used to specify the context is Dojo.hitch. You may never use Dojo.hitch, but you must understand that it is a key part of dojo, and many functions call it internally.

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.