Introduction to some common functions of dojo (II) -- object-oriented (OO) and package system)

Source: Internet
Author: User

Every Ajax framework extends JavaScript and provides many common functions, enhancing the development efficiency of JavaScript. Here we will give a brief introduction to some common functions in Dojo. Because many common functions of dojo are used, these common functions are divided into five categories for your convenience. This article introduces the second part: common functions of object-oriented (OO) and package system.

* Most of the content of this series of blog posts comes from the translation of the dojo reference guide document on dojocampus.org. I would also like to thank the translators of this article: Fei Jia, Zhu Xiao Wen, Li wenbing, zhang Jun, Hu Kuang, Huang Wei, Wu Min Qi, Mo Ying, Cheng Fu, Zhong Si Qi

Dojo. Require

If you want to use dojo as your development kit, you must be familiar with dojo. require. Dojo adopts a namespace and package mechanism similar to the Java language. The Code of dojo is divided into various modules and stored in various packages, if you need to use a module in Dojo, you need to call dojo first. require ("modulename") to pre-load the module. Otherwise, the script will throw an error and prompt "dojo. some not defined ".

Dojo. Require receives a string parameter, which is the name of the module to be loaded. The following is a simple example:

 

// Load dojo/FX. JS: <br/> dojo. require ("dojo. FX "); <br/> // load dojox/widget/toaster. JS: <br/> dojo. require ("dojox. widget. toaster ");

 

In addition, because modules are mutually dependent, it is very difficult for users to judge and load all required modules one by one. Therefore, dojo. require also provides some good features:

  • Automatically load all dependent modules:

For example, dijit. Form. numbertextbox must reference dojo. number. When you use dijit. Form. numbertextbox, you do not need to declare and reference the dojo. Number Module again.

  • Prevent multiple loads:

When a module has been loaded and the same module is declared and referenced again, dojo. Require will return directly without loading the same module again.

  • Use a custom balancer to quickly load the required modules:

You can build customized dojo packages through the packaging mechanism provided by dojo to quickly load modules. You can pre-load modules that are used multiple times to dojo through customization. Because of the prevention mechanism of dojo. Require (), you do not need to modify the code.

So you may ask, do I need to load dojo. Require first through the require mechanism? Of course, the answer is no. All functions in the top-level dojo package are automatically loaded (for example, dojo. Query (), dojo. byid (), and so on ). These are the core methods of dojo and will be frequently used in Dojo reference. Just like the java. lang Package in Java, it does not need to be explicitly declared for loading.

 

Dojo. Provide

In contrast to dojo. Require, dojo. Provide is a function used to provide the dojo Module name. Each dojo class must provide at least one dojo at the beginning of the source code file. provide (), and match the file name. for example, in "JS/dojo/Foo. in the JS "file, call any" dojo. you must add "dojo. provide ('dojo. foo ');".

 

The following Sample Code corresponds to the my/module. js source file. Note that the call to dojo. Provide is always before dojo. require.

 

Dojo. provide ("My. module "); <br/> dojo. require ("dojo. io. script "); <br/> // dojo. provide make sure my. module is created as a JavaScript Object to facilitate attribute assignment <br/> my. module. name = "my module ";

 

Note that multiple dojo. Provide can exist in the same file, but only one dojo. privide () is used in the build script to match the file name correctly.

 

Dojo. Declare

Javascript is different from Java. It does not have a class system, but dojo provides a mechanism to simulate Java class systems: dojo. Declare.
Dojo. Declare accepts the following three parameters:

 

Classname: string, which can be null. The declared class name is used as the global name of the created constructor. If the name is not specified, this class will be considered anonymous (starting from v1.4). If a name is specified, the name will be stored in the created prototype attribute "declaredclass.

 

Superclass: an object or an array of objects. It can also be null (without a base class) and declares the base class of this class. When this parameter is an array, this class has multiple base classes.

 

Props: an object. All attributes of this object are incorporated into the created prototype.

 

In addition, by naming an attribute as "constructor", you can add an initialization function for the created object instance.

 

Example:

 

Dojo. declare ("My. thinger ", null, {<br/> constructor: function (/* object */args) {<br/> dojo. safemixin (this, argS); <br/>}< br/> });

 

Here we declare a simple class that does not inherit any class, called my. thinger. This class eventually contains a constructor.
You can create a my. thinger instance as follows:

 

Dojo. declare ("My. thinger ", null, {<br/> count: 100, <br/> constructor: function (ARGs) {<br/> dojo. safemixin (this, argS); <br/>}< br/>}); <br/> var thing1 = new my. thinger (); <br/> var thing2 = new my. thinger ({count: 200}); <br/> console. log (thing1.count, thing2.count );

 

Dojo. Mixin

Dojo. Mixin is used to mix objects together. The so-called Mixin (mixed in) is to combine two objects from right to left, overwrite the leftmost objects, and return the mixed objects to the user. This Mixin of dojo is very similar to dojo. Extend, but it can only be used for objects, unlike extend, which explicitly extends the prototype of an object (object. Prototype ).

The Mixin function modifies the first object in the list. The second object (and all objects later) is mixed into the first object.

 

VaR A = {B: "C", D: "E" };< br/> dojo. mixin (A, {d: "F", G: "H"}); <br/> console. log (a); // B: C, D: F, G: H

 

If you want to create a new hybrid object, you can use the following methods: first, use dojo. clone to clone the existing object and then mix it in:
VaR newobject = dojo. Mixin (Dojo. Clone (a), B );

 

Another way is to use an empty object as the first parameter and then mix other objects into the empty object. You can repeat this process:

 

VaR newobject = dojo. mixin ({}, B); <br/> dojo. mixin (newobject, c); <br/> dojo. mixin (newobject, dojo. mixin (E, F); <br/> // you can continue to mix in.

 

As long as you remember that the instance of the object as the first parameter will always be rewritten, and the object on the right has a higher priority.

 

Dojo. Extend

The working principle of dojo extend is similar to that of dojo. Mixin. The difference is that it directly acts on the prototype of an object. Like Mixin, dojo. Extend directly merges the members of the objects on the rightmost side of its parameters into the objects of the first parameter.

We can use the extend method to extend the functions of an existing class:

 

Dojo. Require ("dijit. titlepane"); <br/> dojo. Extend (dijit. titlepane, {<br/> randomattriane: "value" <br/> });

 

Dojo. exists

Dojo. exists is used to check whether all objects separated by periods '.' exist in a string, such as A. B .C. Dojo. exists is a convenient method, especially when a long object path needs to be detected. It accepts a string as its first parameter. This method will try to check whether the object contained in this path exists along the path indicated by this string. The second parameter is optional. You can set an object as the root of the path represented by the preceding string. If the second parameter is ignored, it uses the Global object as the root for retrieval by default. Each part of the string separated by '.' is checked for definition. This method returns true only when all objects in the path exist.

The first parameter of dojo. exists is a string indicating the object path to be checked. The second parameter is an optional parameter, which is the root object for searching the object path. Returns a Boolean value.

 

// Check whether a widget exists <br/> var widgettype = "form. button "; <br/> var mynamespace = docs; <br/> If (Dojo. exists (widgettype, mynamespace) {<br/> console. log ("there's a docs. form. button available "); <br/>} else if (Dojo. exists (widgettype, dijit) {<br/> console. log ("dijits form. button class is available "); <br/>} else {<br/> console. log ("no form. button classes are available "); <br/>}

 

Dojo. Clone

Used to clone objects or DOM nodes. This function returns a new cloned object.

 

// Clone object <br/> var OBJ = {A: "B", C: "D" };< br/> var thing = dojo. clone (OBJ); <br/> // clone an array <br/> var newarray = dojo. clone (["A", "B", "C"]);

 

These are some of the common functions of dojo object-oriented (OO) and package system. In the next section, we will introduce some common functions related to the page lifecycle and Dom.

 

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.