I am trying to read the source code of Zepto.js
. And there is a place in function matches
that I don't understand:
zepto.matches = function(element, selector) {
if (!selector || !element || element.nodeType !== 1) return false
var matchesSelector = element.webkitMatchesSelector || element.mozMatchesSelector ||
element.oMatchesSelector || element.matchesSelector
if (matchesSelector) return matchesSelector.call(element, selector)
var match, parent = element.parentNode, temp = !parent
if (temp) (parent = tempParent).appendChild(element)
//Here is my question. This line used the `~`
match = ~zepto.qsa(parent, selector).indexOf(element)
temp && tempParent.removeChild(element)
return match
}
Function matches
is used for determines whether an element matches the selector provided. It should return a Boolean.
And the zepto.qsa()
is Zepto's CSS selector implementation which uses document.querySelectorAll
and some other optimizes.
So. What is the purpose of the ~
in the following code ?
match = ~zepto.qsa(parent, selector).indexOf(element)
I know that ~
means Bitwise NOT
.
And (by my own tests):
~ -1 === 0
~ 0 === -1
~ 1 === -2
But I still don't understand what this design is for.
Can someone explain?