当前位置: 首页 > 图文教程 > 网络编程 > Javascript > Javascript 布尔型分析

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 布尔型分析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 313 ::
收藏到网摘: n/a

今天在做景德镇的企业等级功能的时候,遇到一个很有趣的问题。在一个jsp页面里,我需要把一个java的字符串转为js的布尔型。 本来我以为js的布尔型转换跟java一样,毕竟大家同一个祖宗嘛,以下是我的写法。
function foo() {
var temp = Boolean.valueOf('<%=javaBoolean%>');
alert(temp == false);
}
java变量javaBoolean是一个字符串,它的值是"false",本来我以为肯定输出true,但结果却false,真是郁闷至极。
  刚刚查了一下《javascript权威指南》,才茅塞顿开。原来是这样的:
  如果是想将其它类型转为布尔型,应该用Boolean(value)或new Boolean(value),Boolean.valueOf()这个方法是对象才有的,并不是Boolean的静态方法,还有一点很重要:0、 NaN、null、空字符串和undefined都将转换成false,其它原始值,除了false(但包含字符串"false"),以及其它的对象和数组都将转换为true。
  看到这里,你应该觉得被js忽悠了一把也是值的吧?