javascript JavaScript中变量前的感叹号是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19491491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does an exclamation mark before a variable mean in JavaScript
提问by Winters
I'm trying to learn JavaScript by going through some code in an application and I keep seeing !variable
in if conditions. For example:
我正在尝试通过浏览应用程序中的一些代码来学习 JavaScript,并且我一直!variable
在查看if 条件。例如:
if (!variable.onsubmit || (variable.onsubmit() != false)) {
What is it? Some kind of test if the variable is empty?
它是什么?某种测试变量是否为空?
回答by Benjamin Gruenbaum
!
is the logical not operatorin JavaScript.
!
是JavaScript 中的逻辑非运算符。
Formally
正式地
!expression
is read as:
!expression
读作:
- Take
expression
and evaluate it. In your case that'svariable.onsubmit
- Case the result of that evaluation and convert it to a boolean. In your case since
onsubmit
is likely a function, it means - if the function is null or undefined - return false, otherwise return true. - If that evaluation is true, return false. Otherwise return true.
- 拿去
expression
评价一下。在你的情况下variable.onsubmit
- 将该评估的结果大小写并将其转换为布尔值。在您的情况下,因为
onsubmit
可能是一个函数,这意味着 - 如果该函数为空或未定义 - 返回 false,否则返回 true。 - 如果该评估为真,则返回假。否则返回真。
In your case
在你的情况下
In your case !variable.onsubmit
means return true if there isn't a function defined (and thus is falsy), otherwise return false (since there is a function defined).
在您的情况下,!variable.onsubmit
意味着如果没有定义函数(因此为假)则返回真,否则返回假(因为定义了函数)。
Simply put - !variable
means take the truth value of variable
and negate it.
简单地说 -!variable
意味着取真值variable
并否定它。
Thus, if (!variable) {
will enter the if
clause if variable is false
(or coerces to false)
因此,if (!variable) {
将进入if
子句 if variable is false
(or coerces to false)
In total
总共
if (!variable.onsubmit || (variable.onsubmit() != false)) {
Means - check if variable.onsubmit
is defined and truthy (thus true), then it checks if calling onsubmit
returns a result that coerces to true. In a short line it checks if there is no onsubmit
or it returns true.
手段 - 检查是否variable.onsubmit
已定义且为真(因此为真),然后检查调用是否onsubmit
返回强制为真的结果。在简短的一行中,它检查是否没有onsubmit
或返回 true。
Next time, how do I find this myself?
下一次,我如何自己找到这个?
- MDN has a list of operators here.
- The language specificationspecifies such operators, though being the official specification it does contain some jargon which might be hard to understand.
- MDN 在这里有一个操作员列表。
- 语言规范指定了此类运算符,尽管它是官方规范,但它确实包含一些可能难以理解的术语。
回答by David Barker
It is a negation operator used for truth tests on a variable.
它是用于对变量进行真值测试的否定运算符。
var myVariable = 1;
if ( ! myVariable )
{
// myVariable evaluates as false
}
if ( myVariable )
{
// myVariable evaluates as true
}
回答by Prasanna
The selected answer already answers the question. One thing to add in this is that !
operator can be used in a rather interesting fashion.
所选答案已经回答了问题。要添加的一件事是,!
可以以一种相当有趣的方式使用运算符。
obj = {}
if (!!obj) {console.log('works')} // !!obj = true
obj = undefined
if (!!obj) {console.log('does not work')} // !!obj = false
So double !!
will change any expression to a boolean
value with no exceptions.
所以 double!!
会将任何表达式更改为一个boolean
值,没有例外。
This has a very peculiar use case in some cases.
在某些情况下,这有一个非常特殊的用例。
Lets say there is a function that returns either true or false and nothing else. In that case one could just change
return returnValue
toreturn !!returnValue
for extra caution (although not need in most of the cases)Conversely, if a function only accepts true or false and nothing else, one could just call the function as
functionA(!!parameter)
instead offunctionA(parameter)
, which again is not needed in most of the cases, but could ensure extra security
假设有一个函数返回 true 或 false,没有别的。在这种情况下,可以更改
return returnValue
为return !!returnValue
以格外小心(尽管在大多数情况下不需要)相反,如果一个函数只接受 true 或 false 而没有别的,可以只调用函数 as
functionA(!!parameter)
而不是functionA(parameter)
,这在大多数情况下也不需要,但可以确保额外的安全性