Prototype Usage Note

Source: Internet
Author: User
Try. These (function ...) -> Firateresult

 

 

Accept any number of functions (Note: function reference) and return the first function return value without exception.

This method provides a simple method for sequential execution of manyCodeBlock. Such an attempt queue is usually expressed as a concession function to obtain the specified result.

In the following example from the prototype Ajax library, we want to obtain the xmlhttprequet object. However, IE6 or earlier versions do not provide a native JavaScript Object. If we try to instantiate it, an exception will be thrown. In addition, after a period of time, such a private method has been improved, that is, the name of the COM interface has been changed.

Try. TheseThese methods will be tried sequentially, from the best (theoretically, most common) to the oldest and least usable methods, and then the return value of the first successfully executed function will be returned.

If no code block succeeds, try. These returns an undefined. For example, in the following example, the gettransport method returns false, as if providing a reliable return value.

Gettransport:Function(){

ReturnTry. These (

function () { return New XMLHttpRequest () },

function () { return New activexobject ( 'msxml2. XMLHTTP ' ) },

Function(){Return NewActivexobject ('Microsoft. xmlhttp')}

) |False;

}

 

Document. getelementsbyclassname (classname [, element])-> [htmlelement…]

 

 

Find (and provide) all elements of the CSS class named classname. An optional element parameter is used to specify a parent element for search.

 

Note: Each returned element has been extended.

Example

HTML:

<Body>

<Div id = "one" class = "foo"> single class name </div>

<Div id = "two" class = "foo bar Thud"> multiple class names </div>

<Ul id = "list">

<Li id = "item_one" class = "Thud"> list item 1 </LI>

<Li> list item 2 </LI>

<Li id = "item_two" class = "Thud"> list item 3 </LI>

</Ul>

</Body>

Javascript:

Document. getelementsbyclassname ('Foo');

// Return [htmlelement, htmlelement] (div # One, div # Two)

Document. getelementsbyclassname ('Thud');

// Return [htmlelement, htmlelement, htmlelement] (div # Two, Li # item_one, Li # item_two );

Document. getelementsbyclassname ('Thud', $ ('LIST'));

// Return [htmlelement, htmlelement] (Li # item_one, Li # item_two)

 

$ W (string)-> Array

 

 

Use all spaces as delimiters to split a string into an array. Equivalent to Ruby's % W (FOO bar or Perl's QW (FOO bar ). (Note: Of course, it can also be understood as an encapsulation of string. Prototype. Split)

This is one of those life-savers for people who just hate commas in literal arrays :-)(Note: I don't know how to translate it for ridicule :-()

Examples

$ W ('Apples bananas kiwis')

//-> ['Append', 'banas', 'kiwis ']

It can easily shorten the code when writing a simple iterator:

$ W ('Apples bananas kiwis'). Each (Function(Fruit ){

VaRMessage ='I like'+ Fruit

// Do something with the message

})

In addition, it will become a delicious dessert when collecting element functions:

$ W ('ads navbar funkylinks '). Each (element. Hide );

 

$ R (START, end [, exclusive = false])-> objectrangeCreate a new objectrange object. This method is a convenient encapsulation of the objectrange constructor, but $ R is a good alias.

ObjectrangeThe instance depicts the arrangement of an adjacent value, whether it is a number, text or any syntactically supporting value arrangement. View the type document to get more details, and then find out how to sort the supported values of your own objects.

$ The r function must have the same parameters as the original constructor: lower limit and upper limit (values of the same correct type), and whether the upper limit exists. By default, the upper limit is included.

Example

$ R(0,10). Include (10)

// Return true

$($ R(0,5). Join (',')

// Return '0, 1, 2, 3, 4, 5 ′

$($ R('A','Ah'). Join (',')

// Return 'aa, AB, AC, AD, AE, AF, AG, Ah'

$ R(0,10,True). Include (10)

// Return false

$ R(0,10,True). Each (Function(Value ){

// 10 calls are equivalent to a loop whose value ranges from 0 to 9.

});

Note that objectrange is a mixed enumeration module: this will make it easy to convert the arrangement into an array (enumeration provides a toarray method, which makes the $ A Conversion Function straightforward ), or use the value to iterate.

Note:

However, using the start and end attributes to obtain the range is more efficient than calling the min () and max () methods. (Note: $ R (). Start, $ R (). End)

 

$ H ([OBJ])-> enumerablehash

 

 

Obtain the unique method of a hash (equivalent to ing or union array. Returns a new object with hash and enumeration module methods. If an original object is passed, all its attributes will be cloned before being mixed with the preceding modules.

The $ H method is the only correct method for obtaining a hash.

In fact, hash is a module, and it is not a class added to the prototype class mechanism. Using new hash () will not be helpful, but will lead to a constructor error.

The hash module is designed to be mixed with objects in the mixed enumeration module. Therefore, the $ H function can guarantee that an object is provided for you to mix the above two.

You can call $ H () without parameters to obtain an empty hash (a hash with only mixed module methods ). If you pass it a parameter, it will clone the object passed in before the hybrid module. If any attribute from the object conflicts with the method in the module, it will be deleted.

Currently, there is an unreleased patch for providing secure hash. That is, hash can use any key name, except for the names with risky conflicts or syntax restrictions. This page will be updated later.

 

$ F (element)-> Value
Returns the value of a form control. It is a short alias of form. element. getvalue.

This relatively useful multi-point

 

 

 

 

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.