& Amp; |

Source: Internet
Author: User

In jQuery1.2.6, there is such a method that will be dizzy at first glance in the clean method. I don't know what it says.
"|, & Can be used like this ?", "What is the final returned object of this section ?"
// Trim whitespace, otherwise indexOf won't work as expected
Var tags = jQuery. trim (elem). toLowerCase (), div = context. createElement ("div ");

Var wrap =
// Option or optgroup
! Tags. indexOf ("<opt ")&&
[1, "<select multiple = 'Multiple '>", "</select>"] |

! Tags. indexOf ("<leg ")&&
[1, "<fieldset>", "</fieldset>"] |

Tags. match (/^ <(thead | tbody | tfoot | colg | cap )/)&&
[1, "<table>", "</table>"] |

! Tags. indexOf ("<tr ")&&
[2, "<table> <tbody>", "</tbody> </table>"] |

// <Thead> matched above
(! Tags. indexOf ("<td") |! Tags. indexOf ("<th "))&&
[3, "<table> <tbody> <tr>", "</tr> </tbody> </table>"] |

! Tags. indexOf ("<col ")&&
[2, "<table> <tbody> </tbody> <colgroup>", "</colgroup> </table>"] |

// IE can't serialize <link> and <script> tags normally
JQuery. browser. msie &&
[1, "div <div>", "</div>"] |

[0, "", ""];

After in-depth research and query of the data, I realized that what I want to do at a low level is also overwhelmed by the writing of this huge bull.


// Logical AND &: the second operand will always be returned, no matter whatever it is, doesn t the first operand is one of (0,-0, null ,"", false, undefined, NaN) for such condition, the first operand will be returned.

// Logical OR |: the first operand will always be returned, cannot the first operand is one of (0,-0, null, "", false, undefined, NaN ).
// In this situation the second operand will be returned, no matter what the second operand it is, even it's the same to the first one.


// <Professional JavaScript for Web Developers 2nd Edition.pdf> Page 52.

Because trim has passed, there is no space between the front and back, mainly because there is no space in front of the string, so at this time to determine whether to start with what is startWith, the simplest is to write it as tags. indexOf ("<opt"), view indexOf, return value when startWith is true, just return 0, other cases: 1, the return value that appears in the string is greater than 0. 2. If the string is not out of date, the return value is-1. the same number is a non-zero number. However, in the JavaScript definition, the number class object is regarded as false only when it is set to 0, all other negative values are considered as true.

The indexOf () method returns the position of the first occurrence of a specified value in a string.

If the Boolean object has no initial value, or if the passed value is one of the following:

0,-0, null, "", false, undefined, NaN
The object it is set to false. For any other value it is set to true (even with the string "false ")!

This method returns-1 if the value to search for never occurs.

Assume that if the tag starts with "<opt", the value of indexOf is 0, and the first one is added! After ,! The value of tag. indoexOf ("<opt") is true.
True & [...], let's look back at the definition of &. If the previous value is false, the previous parameter value is returned. Otherwise, the second parameter is always returned (even if it is false or NaN ).

In short, in the original jQuery code, the two logical symbols are frequently used, especially |.
There are almost no implementation methods in jQuery. Using them can make the code simpler, more elegant, and more efficient.

For example, the lower part is often used and has a default value. A doesn't work. If B is on B and B is not running, then only c can be used. The second processing method.
// Start an animation from one number to another
Custom: function (from, to, unit ){
This. startTime = now ();
This. start = from;
This. end =;
This. unit = unit | this. unit | "px ";

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.