Shorten JS if or statement

This question already has an answer here: How do I check if an array includes an object in JavaScript? 40 answers You can use use Array.indexOf [1,2,3,4].indexOf(x) !== -1 You can also use objects as some kind of hash map: //Note: Keys will be coerced to strings so // don't use this method if you are looking for an object or if you need // to distinguish the number 1 from the string "1" m

缩短JS if或语句

这个问题在这里已经有了答案: 如何检查数组是否包含JavaScript中的对象? 40个答案 你可以使用使用Array.indexOf [1,2,3,4].indexOf(x) !== -1 你也可以使用对象作为某种散列图: //Note: Keys will be coerced to strings so // don't use this method if you are looking for an object or if you need // to distinguish the number 1 from the string "1" my_values = {1:true, 2:true, 3:true, 'foo':true} my_valu

How do I change file extension with javascript

Does anyone know an easy way to change a file extension in Javascript? For example, I have a variable with "first.docx" but I need to change it to "first.html". This will change the string containing the file name; file = file.substr(0, file.lastIndexOf(".")) + ".htm"; For situations where there may not be an extension: var pos = file.lastIndexOf("."); file = file.subst

如何使用JavaScript更改文件扩展名

有谁知道一个简单的方法来更改JavaScript中的文件扩展名? 例如,我有一个变量“first.docx”,但我需要将其更改为“first.html”。 这将改变包含文件名的字符串; file = file.substr(0, file.lastIndexOf(".")) + ".htm"; 对于可能没有扩展名的情况: var pos = file.lastIndexOf("."); file = file.substr(0, pos < 0 ? file.length : pos) + ".htm"; file = file.replace(/.[^.]+$/, '.html'); 这可能不会得到许多upvo

How to check if the URL contains a given string?

我怎么能这样做: <script type="text/javascript"> $(document).ready(function () { if(window.location.contains("franky")) // This doesn't work, any suggestions? { alert("your url contains the name franky"); } }); </script> 您需要添加href属性并检查indexOf而不是contains <script type="text/javascript"> $(document).ready(function () { if(window.location.href.in

如何检查URL是否包含给定的字符串?

我怎么能这样做: <script type="text/javascript"> $(document).ready(function () { if(window.location.contains("franky")) // This doesn't work, any suggestions? { alert("your url contains the name franky"); } }); </script> 您需要添加href属性并检查indexOf而不是contains <script type="text/javascript"> $(document).ready(function () { if(window.location.href.in

What is "~" represent in this JavaScript?

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers It's bitwise NOT : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators "take the result of the indexOf lookup, bitwise invert it, then take the logical NOT of that"

什么是“〜”代表这个JavaScript?

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 它是按位NOT :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators “采取indexOf查找的结果,按位倒置它,然后采取逻辑NOT的那个”

tilde ~ in front of anonymous function in JavaScript

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers JavaScript plus sign in front of function name 3 answers

在JavaScript中的匿名函数前徘徊〜

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 JavaScript加上函数名称前面的3个答案

What does ~[Array].indexOf(key) do?

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers What exactly does ~ do? [duplicate] 3 answers indexOf returns -1 if not found. ~-1 is 0 , ie false . So the whole things means: if key is found in the array, do continue.

〜[Array] .indexOf(key)做什么?

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 到底做什么? [重复] 3个答案 如果未找到, indexOf返回-1 。 ~-1是0 ,即false 。 因此,整个事情意味着:如果在数组中找到 key ,请继续。

What exactly does ~ do?

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers It's the bitwise NOT operator. It will convert the operand to an 32-bit integer, then yields one's complement (inverts every bit) of that integer. Finally, ! will return true if and only only if the result of that operation is 0 . Some examples might help: x | x (bin) |

到底做什么?

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 它是按位运算符。 它会将操作数转换为32位整数,然后产生该整数的补码(反转每一位)。 最后! 当且仅当该操作的结果为0才会返回true 。 一些例子可能有所帮助 x | x (bin) | ~x (bin) | ~x | !~x -3 | 1111…1101 | 0000…0010 | 2 | false -2 | 1111…1110 | 0000…0001 | 1 | false -1 | 1111…1111 | 0000…0000 | 0 | true 0

What does ~fs.readdirSync mean in config files?

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers ~ is the bitwise NOT operator NOT. Every bit in x is inverted in ~x . For instance: x = 00011011 ~x = 11100100 Alternatively, it is equivalent to doing XOR with 0xFFFFFFFF (all 1-bits): 00011011 XOR 11111111 -------- = 11100100

〜fs.readdirSync在配置文件中的含义是什么?

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 ~是按位NOT运算符NOT。 在每一位x被倒~x 。 例如: x = 00011011 ~x = 11100100 或者,它相当于使用0xFFFFFFFF (全1位)进行XOR: 00011011 XOR 11111111 -------- = 11100100

what's the " !~" mean in javascript

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers tl;dr indexOf returns -1 when an element cannot be found in an array. Therefore, the if statement is checking if name could not be found in names . !~-1 ==> true Longer version: The tilde ( ~ ) operator (bitwise NOT) yields the inverted value (aka one's complement) of a. [S

JavaScript中的“!〜”是什么意思?

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 TL;博士 当在数组中找不到元素时, indexOf返回-1。 因此, if语句正在检查name是否找不到names 。 !~-1 ==> true 更长的版本: 波浪号( ~ )运算符(按位NOT)产生a的倒数值(又称补码)。 [Source]例如, ~-1 === 0 。 请注意, 0 == false和!0 === true 。 当在数组中找不到元素时, indexOf返回-1。 因此,我们可以用!~-1 === tru

Why "~undefined" is

This question already has an answer here: What does a tilde do when it precedes an expression? 6 answers ~ is bitwise NOT. It uses ToInt32 to convert the argument to a number. ToInt32 is defined as: Let number be ToNumber(argument). ReturnIfAbrupt(number). If number is NaN, +0, −0, +∞, or −∞, return +0. ... In turn, ToNumber(undefined) returns NaN , so according to step 3, ToInt3

为什么“〜undefined”是

这个问题在这里已经有了答案: 在表达式之前,代字号在做什么? 6个答案 ~是按位不是。 它使用ToInt32将参数转换为数字。 ToInt32被定义为: 让数字为ToNumber(参数)。 ReturnIfAbrupt(数字)。 如果数字是NaN,+ 0,-0,+∞或-∞,则返回+0。 ... 反过来, ToNumber(undefined)返回NaN ,所以根据步骤3, ToInt32返回0 。 并且~0是-1 。 不能在位表示在JS例如每一件事情“未定义,NAN”被视为一个0或000000