Prototype tool function learning _ prototype

Source: Internet
Author: User
Prototype Learning Tool functions $ H, $ R, Try. these, document. getElementsByClassName $ H are convenient methods for creating Hash objects. For details about Hash objects, refer to [Prototype Learning-Hash objects]
$ R is a convenient method for resume ObjectRange objects. For details about ObjectRange objects, refer to [Prototype Learning-ObjectRange objects]
Try. these:
Accepts an arbitrary number of functions and returns the result of the first one that doesn't throw an error.

The Code is as follows:


// Use a loop nested try... catch to complete the function of this tool.
Var Try = {
These: function (){
Var returnValue;
For (var I = 0, length = arguments. length; I <length; I ++ ){
Var lambda = arguments [I];
Try {
ReturnValue = lambda ();
Break;
} Catch (e ){}
}
Return returnValue;
}
};


Let's look at an example (different browsers have different methods for creating XMLHttpRequest ):

The Code is as follows:


GetTransport: function (){
Return Try. these (
Function () {return new XMLHttpRequest ()},
Function () {return new ActiveXObject ('msxml2. xmlhttp ')},
Function () {return new ActiveXObject ('Microsoft. xmlhttp ')
}) | False ;}


Document. getElementsByClassName ():
You can probably guess the purpose of this method based on the method name. However, this method is marked
Deprecated. It is replaced by the $ and Eelement. select methods. The two methods are described later.

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.