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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:45:55  来源:igfitidea点击:

What does an exclamation mark before a variable mean in JavaScript

javascriptvariables

提问by Winters

I'm trying to learn JavaScript by going through some code in an application and I keep seeing !variablein 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

正式地

!expressionis read as:

!expression读作:

  • Take expressionand evaluate it. In your case that's variable.onsubmit
  • Case the result of that evaluation and convert it to a boolean. In your case since onsubmitis 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.onsubmitmeans 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 - !variablemeans take the truth value of variableand negate it.

简单地说 -!variable意味着取真值variable并否定它。

Thus, if (!variable) {will enter the ifclause 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.onsubmitis defined and truthy (thus true), then it checks if calling onsubmitreturns a result that coerces to true. In a short line it checks if there is no onsubmitor it returns true.

手段 - 检查是否variable.onsubmit已定义且为真(因此为真),然后检查调用是否onsubmit返回强制为真的结果。在简短的一行中,它检查是否没有onsubmit或返回 true。

Next time, how do I find this myself?

下一次,我如何自己找到这个?

回答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 booleanvalue 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 returnValueto return !!returnValuefor 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 of functionA(parameter), which again is not needed in most of the cases, but could ensure extra security

  • 假设有一个函数返回 true 或 false,没有别的。在这种情况下,可以更改return returnValuereturn !!returnValue以格外小心(尽管在大多数情况下不需要)

  • 相反,如果一个函数只接受 true 或 false 而没有别的,可以只调用函数 asfunctionA(!!parameter)而不是functionA(parameter),这在大多数情况下也不需要,但可以确保额外的安全性