A little doubt in the course of Learning "contains () and Comparedocumentpositon ()"!

Source: Internet
Author: User
Tags bitmask

JS developers often need to determine whether a node contains another node, ie first introduced the CONTAINS () function, then, Safari3 and later, Opera 8 and later, Chrome supports this method (Safari 2. Although this method is supported in X, it does not work properly). Firefox does not support this method, but provides an alternative method comparedocuemntposition () in DOM3. For this reason, to cross-platform determine whether a node contains another node, it is necessary to provide a common contains () function to resolve the difference, the usual solution is as follows:

  var function (A, b) {     return a.contains? A! = B && A.contains (b):!! (A.comparedocumentposition (b) &);}

In the above code, the three-mesh operator is used to determine if the browser supports the contains () method, and if it is supported, a! = B && A.contains (b) is judged and a Boolean value is returned, otherwise

!! (A.comparedocumentposition (b) & 16) to judge, my doubts are in this, and we know that the logical non-operator consists of an exclamation mark (!). Indicates that it can be used for any value in the ECMAScript, regardless of what data type it is, this operator returns a Boolean value. Use two logical non (!) simultaneously operator, it simulates the behavior of the Boolean () transformation function, so it gets the Boolean value that the value really corresponds to. In other words, the actual judgment is A.comparedocumentposition (b) & 16. A bitmask representing the relationship is returned when using the Comparedocumentposiotn () method, and the following table lists the possible values for this bitmask:

Mask Node relationships
1 Irrelevant (the given node is not in the current document)
2 Pre (node of the given point before the node of the DOM tree in the given point)
4 Post (node of the given point after the DOM tree species is located at the node of the given point)
8 Contains (the given node is the ancestor of the reference node)
16 is included (the given node is the descendant of the reference node)

To mimic contanins (), it should be concerned with 16. If the value of A.comparedocumentposition (b) is 1,2,4,8,16, then!! (1&16),!! (2&16),!! (4&16) ... How to confirm it? Here we have to pay attention to the "&" operator, which is called "bitwise-and", essentially, the bitwise-and is to align each bit of the two numeric values, and then according to the rules in the following table, the two numbers at the same location are manipulated:

Bits of the first value Bits of the second value Results
1 1 1
1 0 0
0 1 0
0 0 0

Take 16&16 as an example, first convert 16 to binary, 16=0000 0000 0000 0000 0000 0000 0001 0000, then 16&16=0000 0000 0000 0000 0000 0000 0001 0000 (Specific The operation process can refer to the principle of the bitwise operator), then!! (16&16) =true (for a few other masks, you might as well try it with 16 bitwise AND the result of the operation is 0). So through the above methods, you can pass!! (A.comparedocumentposition (b) & 16) simulates the A.contanins (b).

A little doubt in the course of Learning "contains () and Comparedocumentpositon ()"!

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.