問題:
If you read the comments at the jQuery inArray
page here , there's an interesting declaration:如果您在這裏閱讀 jQuery inArray
頁面上的評論,會發現一個有趣的聲明:
!!~jQuery.inArray(elm, arr)
Now, I believe a double-exclamation point will convert the result to type boolean
, with the value of true
.現在,我相信雙感嘆號會將結果轉換爲boolean
類型,值爲true
。 What I don't understand is what is the use of the tilde ( ~
) operator in all of this?我不明白的是,所有這些中波浪號 ( ~
) 運算符的用途是什麼?
var arr = ["one", "two", "three"];
if (jQuery.inArray("one", arr) > -1) { alert("Found"); }
Refactoring the if
statement:重構if
語句:
if (!!~jQuery.inArray("one", arr)) { alert("Found"); }
Breakdown:分解:
jQuery.inArray("one", arr) // 0
~jQuery.inArray("one", arr) // -1 (why?)
!~jQuery.inArray("one", arr) // false
!!~jQuery.inArray("one", arr) // true
I also noticed that if I put the tilde in front, the result is -2
.我還注意到,如果我把波浪號放在前面,結果是-2
。
~!!~jQuery.inArray("one", arr) // -2
I don't understand the purpose of the tilde here.我不明白這裏波浪號的目的。 Can someone please explain it or point me towards a resource?有人可以解釋一下或指出我的資源嗎?