在javascript中返回!1 [英] return !1 in javascript

查看:117
本文介绍了在javascript中返回!1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了一个JavaScript函数,它有 return!1



我只是想知道这实际上意味着什么?



为什么你会返回!1 return!0



请问有人请解释一下这是什么意思?

碰到:

pre $ 函数convertStringToBoolean(a){
typeof a ==string&& (a = a.toLowerCase());
switch(a){
case1:
casetrue:
caseyes:
casey:
case 1:
case!0:
return!0;
default:
return!1
}
}

感谢提前!

解决方案

return!1 表示 return false return!0 - 返回true 。在规范中 - 当您放置时, 11.4.9逻辑非运算符 - !在前面结果评估为布尔值,并且返回相反的结果。



例子:

  var a = 1,b = 0; 
var c = a || b;
alert(c =+ c ++ typeof c); //这里c将数字
a =!0,b =!1;
c = a || b;
alert(c =+ c ++ typeof c); //这里是布尔

我主要在通过谷歌优化器传递的代码中看到这一点。我认为这主要是为了达到代码的简短性。



当需要布尔结果时经常使用它 - 您可能会看到类似于的东西! !(表达式)。例如,在jQuery中搜索。


I have just come across a function in javascript which has return !1

I was just wondering what this actually meant?

Why would you return !1 or return !0

Could someone please explain what it means please?

Here is the function that I came across:

function convertStringToBoolean(a) {
    typeof a == "string" && (a = a.toLowerCase());
    switch (a) {
    case "1":
    case "true":
    case "yes":
    case "y":
    case 1:
    case !0:
        return !0;
    default:
        return !1
    }
}

Thanks In advance!

解决方案

return !1 means return false and return !0 - return true. In the specification - 11.4.9 Logical NOT Operator - when you place ! in front the result is evaluated as Boolean and it the opposite is returned.

Example:

var a = 1, b = 0;
var c = a || b;
alert ( "c=" + c + " " + typeof c ); // here c will be number
a = !0, b = !1;
c = a || b;
alert ( "c=" + c + " " + typeof c ); // here it will be boolean

I mostly see this in a code passed through google's optimizer. I think it is mostly done to achieve shortness of the code.

It is often used when boolean result is needed - you may see something like !!(expression). Search in jQuery, for example.

这篇关于在javascript中返回!1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆