仅使用var在JavaScript中声明布尔值
如果我声明如下JavaScript布尔变量:仅使用var在JavaScript中声明布尔值,javascript,boolean,Javascript,Boolean,如果我声明如下JavaScript布尔变量: var IsLoggedIn; var MyNamespace = { convertToBoolean: function (value) { //VALIDATE INPUT if (typeof value === 'undefined' || value === null) return false; //DETERMINE BOOLEAN VALUE FROM STRING
var IsLoggedIn;
var MyNamespace = {
convertToBoolean: function (value) {
//VALIDATE INPUT
if (typeof value === 'undefined' || value === null) return false;
//DETERMINE BOOLEAN VALUE FROM STRING
if (typeof value === 'string') {
switch (value.toLowerCase()) {
case 'true':
case 'yes':
case '1':
return true;
case 'false':
case 'no':
case '0':
return false;
}
}
//RETURN DEFAULT HANDLER
return Boolean(value);
}
};
MyNamespace.convertToBoolean('true') //true
MyNamespace.convertToBoolean('no') //false
MyNamespace.convertToBoolean('1') //true
MyNamespace.convertToBoolean(0) //false
然后用
true
或1
对其进行初始化,这样安全吗?或者用1
初始化它会使变量成为一个数字吗?否这是不安全的。您可以稍后执行varIsLoggedIn=“Foo”
和JavaScript不会抛出错误
这是可能的
var IsLoggedIn = new Boolean(false);
var IsLoggedIn = new Boolean(true);
您还可以将非布尔变量传递到new boolean()
,它将使IsLoggedIn成为布尔变量
var IsLoggedIn = new Boolean(0); // false
var IsLoggedIn = new Boolean(NaN); // false
var IsLoggedIn = new Boolean("Foo"); // true
var IsLoggedIn = new Boolean(1); // true
如果希望将
IsLoggedIn
视为布尔值,则应按如下方式初始化:
var IsLoggedIn=true;
如果使用var IsLoggedIn=1初始化它代码>则它将被视为一个整数
但是,变量IsLoggedIn
在任何时候都可能引用不同的数据类型:
IsLoggedIn="Hello World";
这不会导致错误。类型取决于您的初始化:
var IsLoggedIn1 = "true"; //string
var IsLoggedIn2 = 1; //integer
var IsLoggedIn3 = true; //bool
但看看这个例子:
var IsLoggedIn1 = "true"; //string
IsLoggedIn1 = true; //now your variable is a boolean
变量的类型取决于JavaScript中指定的值。变量将成为您指定的类型。最初它是未定义的
。如果指定它'true'
它将成为一个字符串,如果指定它true
它将成为一个布尔值,如果指定它1
它将成为一个数字。以后的赋值可能会更改变量的类型。Javascript中的变量没有类型。非零、非空、非空和true
为“true”。零、空、未定义、空字符串和false
为“false”
不过有一种布尔类型,如文本true
和false
您至少可以使用和测试未初始化变量的“定义性”。
像这样:
var iAmNotDefined;
alert(!iAmNotDefined); //true
//or
alert(!!iAmNotDefined); //false
此外,还有许多可能性:如果您对确切类型不感兴趣,请使用“==”运算符(或![variable]/!![variable])进行比较(我认为这就是道格拉斯·克罗克福德所谓的“truthy”或“falsy”)。在这种情况下,如果将true或1或“1”赋值给单位化变量,则在被询问时总是返回true。否则[如果需要类型安全比较]使用“==”进行比较
var thisMayBeTrue;
thisMayBeTrue = 1;
alert(thisMayBeTrue == true); //=> true
alert(!!thisMayBeTrue); //=> true
alert(thisMayBeTrue === true); //=> false
thisMayBeTrue = '1';
alert(thisMayBeTrue == true); //=> true
alert(!!thisMayBeTrue); //=> true
alert(thisMayBeTrue === true); //=> false
// so, in this case, using == or !! '1' is implicitly
// converted to 1 and 1 is implicitly converted to true)
thisMayBeTrue = true;
alert(thisMayBeTrue == true); //=> true
alert(!!thisMayBeTrue); //=> true
alert(thisMayBeTrue === true); //=> true
thisMayBeTrue = 'true';
alert(thisMayBeTrue == true); //=> false
alert(!!thisMayBeTrue); //=> true
alert(thisMayBeTrue === true); //=> false
// so, here's no implicit conversion of the string 'true'
// it's also a demonstration of the fact that the
// ! or !! operator tests the 'definedness' of a variable.
PS:你不能测试不存在的变量的“定义性”。因此:
alert(!!HelloWorld);
给出引用错误('未定义HelloWorld')
(有没有更好的词来形容“定义性”?请原谅我的荷兰语;~)像这样的词怎么样:
var IsLoggedIn;
var MyNamespace = {
convertToBoolean: function (value) {
//VALIDATE INPUT
if (typeof value === 'undefined' || value === null) return false;
//DETERMINE BOOLEAN VALUE FROM STRING
if (typeof value === 'string') {
switch (value.toLowerCase()) {
case 'true':
case 'yes':
case '1':
return true;
case 'false':
case 'no':
case '0':
return false;
}
}
//RETURN DEFAULT HANDLER
return Boolean(value);
}
};
MyNamespace.convertToBoolean('true') //true
MyNamespace.convertToBoolean('no') //false
MyNamespace.convertToBoolean('1') //true
MyNamespace.convertToBoolean(0) //false
然后你可以这样使用它:
var IsLoggedIn;
var MyNamespace = {
convertToBoolean: function (value) {
//VALIDATE INPUT
if (typeof value === 'undefined' || value === null) return false;
//DETERMINE BOOLEAN VALUE FROM STRING
if (typeof value === 'string') {
switch (value.toLowerCase()) {
case 'true':
case 'yes':
case '1':
return true;
case 'false':
case 'no':
case '0':
return false;
}
}
//RETURN DEFAULT HANDLER
return Boolean(value);
}
};
MyNamespace.convertToBoolean('true') //true
MyNamespace.convertToBoolean('no') //false
MyNamespace.convertToBoolean('1') //true
MyNamespace.convertToBoolean(0) //false
我还没有测试过它的性能,但是从一种类型到另一种类型的转换不应该太频繁,否则你会打开你的应用程序 正如上面所说:
这确实取决于“安全”的含义,javascript中没有“安全”;IsLoggedIn将在所有这些情况下计算为true(是,新布尔值(false)为true)。@Miles:new Boolean(false)不是falsetrue@jan:if(new Boolean(false))警报('wat')
请参见@Miles inclusible,如(new Boolean(false))。toString())===“false”
,感谢您提供的链接,您可能希望使用空字符串重试字符串大小写thisMayBeTrue=''代码>-由于空字符串无效,因此不会得到相同的结果。“PS:你不能为不存在的变量测试‘定义’”-当然可以:typeof HellowWorld===‘undefined’
。JS变量没有一种类型,即你不能声明一个只包含整数或字符串的变量(就像在其他一些语言中发生的那样),但变量在任何给定时间持有的特定值都将始终具有类型。类型的重要性可以用一个简单的例子来说明,比如2+2
vs“2”+2
。另请参见。如果我直接将布尔变量声明为布尔变量会怎么样。这能提高绩效吗?为什么会投反对票?请解释一下,这样我们大家都能受益。可能没有真正的解释。没有人想梳理你可以在tl中指定的教程;博士在你的岗位上