!!~(不是波浪號/bang bang 波浪號)如何改變“包含/包含”數組方法調用的結果?

問題:

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?有人可以解釋一下或指出我的資源嗎?


解決方案:

參考: https://stackoom.com/en/question/d5fw
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章