日期:2014-05-16  浏览次数:20382 次

解释一下 为什么会是这样的!?
document.write( 'Boolean( " ")= '   +   Boolean( " ")+ ' <br> ');
document.write( 'Boolean( "     ")= '+Boolean( "     ")+ ' <br> ');
document.write( '( "     "   ==   false)= '+( "     "   ==   false)+ ' <br> ');
document.write( '( " "   ==   false)= '+( " "   ==   false)+ ' <br> ');
document.write( '(false   ==   false)= '+(false   ==   false)+ ' <br> ');

================================================================
结果:

Boolean( " ")=false
Boolean( "   ")=true
( "   "   ==   false)=true
( " "   ==   false)=true
(false   ==   false)=true

===============================================================
其中
Boolean( "   ")=true
( "   "   ==   false)=true
难道是Bug?!

高手帮忙解释一下~

------解决方案--------------------
Boolean Object

The Boolean object is an object wrapper for a Boolean value.

The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).

We define a Boolean object with the new keyword. The following code line defines a Boolean object called myBoolean:

var myBoolean=new Boolean()

Note: If the Boolean object has no initial value or if it is 0, -0, null, " ", false, undefined, or NaN, the object is set to false. Otherwise it is true (even with the string "false ")!

All the following lines of code create Boolean objects with an initial value of false:

var myBoolean=new Boolean()
var myBoolean=new Boolean(0)
var myBoolean=new Boolean(null)
var myBoolean=new Boolean( " ")
var myBoolean=new Boolean(false)
var myBoolean=new Boolean(NaN)

And all the following lines of code create Boolean objects with an initial value of true:

var myBoolean=new Boolean(true)
var myBoolean=new Boolean( "true ")
var myBoolean=new Boolean( "false ")
var myBoolean=new Boolean( "Richard ")
------解决方案--------------------
Boolean(value):强制转换为boolean类型
alert(Boolean( ' ')); //false
alert(Boolean( 'hello ')); //true
alert(Boolean(100)); //true
alert(Boolean(0)); //false
alert(Boolean(NaN)); //false
alert(Boolean(null)); //false
alert(Boolean(undefined)); //false
alert(Boolean(new Object())); //true

new Boolean(value):
创建初始值为value的Boolean对象
其值为 false、0、null、 NaN,或者空字符串,则该 Boolean 对象的初始值为 false
否则,初始值为 true
就是说在创建new Boolean()对象的时候,先强制转型value值为Boolean类型