Some common functions of dojo (I)-Basic JavaScript extension Functions

Source: Internet
Author: User
Tags javascript extension

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 first part: Basic JavaScript extension functions.

 

* 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. Hitch

Dojo. Hitch is an elegant function. It returns a function that executes the specified statement in the specified scope. By calling this function, you can control function execution well, especially when asynchronous operations are involved. For example:

 

VaR myobj = {<br/> FOO: "bar", <br/> method: function (somearg) {<br/> console. log (this. foo + "" + data); <br/>}< br/>}; <br/> dojo. xhrget ({<br/> URL: "/something. PHP ", <br/> load: myobj. method <br/>}); <br/>

 

 

The preceding example cannot run correctly. You only get a semantic Fuzzy Error message. For example, foo is a variable-defined error. The reason is that, in the asynchronous callback function like the above example, When you assign it an associated table, you change the scope of "foo. its scope is no longer associated with the object that originally generated it, The xhr parameter object. To solve this problem, you can use hitch to force this function to retain its original scope. The correct method should be like this:

 

VaR myobj = {<br/> FOO: "bar", <br/> method: function (data) {<br/> console. log (this. foo + "" + data); <br/>}< br/>}; <br/> dojo. xhrget ({<br/> URL: "/something. PHP ", <br/> load: dojo. hitch (myobj, "method") <br/> });

 

Dojo. Hitch requires two parameters. The first parameter specifies the associated scope of the function call, and the second parameter is the member function name of a function object or the first object.
(Note: For more information about dojo. xhrget, see the subsequent ajaxio section)

Dojo. Partial

Have you ever thought about controlling the parameters passed to the function? For example, if you have such a requirement, set the first parameter passed to the function to be a defined value, while other parameters are still changeable. Dojo provides a way to meet this requirement. Similar to dojo. Hitch, dojo. Partial is also a function that returns a function. What it does is to allow you to lock the first few parameters of a function with a predefined value. This is a very powerful function, especially when you need to input object references or other similar notification functions for data storage.

The following is an example of using partial:

 

VaR dataloaded = function (somefirstparam, Data, ioargs) {}; <br/> var ARGs ={< br/> URL: "foo", <br/> load: dataloaded <br/>}; <br/> dojo. xhrget (ARGs );

 

Then, when the xhrget function returns, it will call the dataloaded function, but the xhrget load function expects the parameter structure to be load (data, ioargs ). So when the first parameter of dataloaded has been determined to be "somefirstparam", how can we ensure that xhrget meets the parameter requirements? Yes, use dojo. Partial! Let's see how you can use dojo. Partial:

 

VaR dataloaded = function (somefirstparam, Data, ioargs) {}; <br/> var ARGs ={< br/> URL: "foo", <br/> load: dojo. partial (dataloaded, "firstvalue"); <br/>}; <br/> dojo. xhrget (ARGs );

It creates a new function, which encapsulates the dataloaded function and specifies the first parameter "firstvalue ". It should be noted that dojo. Partial allows you to specify multiple parameters, so you can define any number of parameters as fixed front parameters of the function.

Dojo. isstring, dojo. isarray, dojo. isfunction, dojo. isobject, dojo. isarraylike, dojo. isalien

As the name suggests, these functions are used to determine whether a given parameter is of a specific type. If yes, true is returned; otherwise, false is returned.

Dojo. isstring: determines whether a given parameter is a string.

Dojo. isarray: determines whether an object is a real array. Note that dojo. isarray checks whether the passed value is an instance created by the array constructor in the current frame. If an array comes from a different framework, it is not an array of the current framework, so dojo. isarray returns false. In addition, the arguments object is not an array. You can use dojo. isarraylike (value) to detect objects similar to arrays.

Dojo. isfunction: Check whether the parameter is a function object. Note that this is also effective for classes (constructors) created by dojo. Declare. A common pattern is to get the full name of a class in string form. You can use dojo. isfunction to determine whether to convert it into a function:

 

// You Need To dynamically use a class <br/> var thing = "dijit. dijit"; <br/> // first check whether it is a function <br/> If (! Dojo. isfunction (thing) {<br/> thing = dojo. getObject (thing); <br/>}< br/> var dialog = new thing ({Title: "bar "});

 

Of course, you can also use dojo. isstring to do similar things.

Dojo. isalien is used to check whether the passed parameter is a built-in function.

Dojo. isobject is used to check whether the passed parameter is an object.

 

The above are some basic extensions of JavaScript by dojo. In the next section, we will introduce some common functions of dojo for Object-Oriented (OO) and package system.

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.