简体   繁体   English

为什么传递给函数时没有解释布尔值false参数?

[英]Why is a boolean false parameter not interpreted when passing to a function?

I did some research but I didn't find a satisfying answer yet. 我做了一些研究,但我还没有找到令人满意的答案。 I have a javascript project file and a library where some functions are defined like this: 我有一个javascript项目文件和一个库,其中一些函数定义如下:

//Library.js 
FElib = {

        initLib: function(param1, param2){
            var init = param1 || true;
            console.log(init);
        }
}

Now in my project.js file I call the function and pass false as a parameter 现在在我的project.js文件中,我调用该函数并将false作为参数传递

FElib.initLib(false, 'test');

In this case param1 is giving me 'false' if I log it before the variable declaration. 在这种情况下,如果我在变量声明之前记录它,param1会给我'false'。 Afterwards it's using the default 'true'. 之后它使用默认的'true'。 It seems like it is interpreting false as undefined were I thought that it's only checking for undefined.. 它似乎是解释错误的未定义我认为它只检查undefined ..

Why is the behaviour like this? 为什么这样的行为?

When I'm using a string it's perfectly working. 当我使用字符串时,它完全正常工作。 Using 0 or 1 as a parameter is also not working and it's using the default variable declaration.. 使用0或1作为参数也不起作用,并且它使用默认变量声明。

As a workaround I declared the variable as an object which is workin fine again like so: 作为一种解决方法,我将变量声明为一个对象,它再次正常工作:

//Library.js
FElib = {

        initLib: function(options){
            var defaults = {
                param1:      true,
                param2:      'string',
            };
            var options = $.extend({}, defaults, options);
        }
}

Now calling the function is working fine even with boolean arguments: 现在调用函数即使使用布尔参数也能正常工作:

FElib.initLib({'param1':false, 'param2':'string123'});

The 2nd approach seems to have a little overhead thinking of a function with only 1 boolean variable as an argument. 第二种方法似乎只有一点开销,只考虑一个布尔变量作为参数。

Can anybody explain this behaviour? 任何人都可以解释这种行为吗?

UH... because init = param1 || true UH ...因为init = param1 || true init = param1 || true isn't checking for undefinedness, it's checking for falsiness. init = param1 || true不检查undefinedness,它检查falsiness。

false is, obviously, falsy, and it is therefore replaced with true. 显然,假是虚假的,因此它被替换为真。

If you want to check for undefinedness, there is no real shorthand to it: 如果你想检查undefinedness,它没有真正的简写:

var init = typeof param1 === "undefined" ? true : param1;

The problem is here: 问题出在这里:

var init = param1 || true;

The boolean OR operator ( || ) returns true there, since it's evaluating: false || true 布尔OR运算符( || )在那里返回true ,因为它正在评估: false || true false || true ; false || true
"False or True" is always "True" . “假或真”总是“真实”

If you want to check if the parameter is undefined, use something like this: 如果要检查参数是否未定义,请使用以下内容:

var init = (typeof param1 === "undefined") ? true : param1;

Because the || 因为|| operator is testing for truthyness, not definedness. 运营商正在测试真实性,而非定义性。

You will get the right hand side of the || 您将获得||的右侧 operator if the left hand side evaluates as false, which false does. 运算符,如果左侧评估为false,则为false

If you want to test if a value is defined use: 如果要测试是否定义了值,请使用:

var init = param1;
if (typeof param1 === "undefined") {
    init = true;
}

or 要么

var init = (typeof param1 === "undefined") ? true : param1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM