Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网站建设 > ASP.NET > JavaScript对象之Boolean
【标  题】:JavaScript对象之Boolean
【关键字】:JavaScript,Boolean
【来  源】:http://blog.csdn.net/5257007/archive/2006/09/27/1296125.aspx

JavaScript对象之Boolean

Your Ad Here

Boolean

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


Core object  

 

Implemented in  

JavaScript 1.1, NES 2.0

JavaScript 1.3: added toSource method  

ECMA version  

ECMA-262  


Created by
The Boolean constructor:

new Boolean(value)


Parameters


value

 

The initial value of the Boolean object. The value is converted to a boolean value, if necessary. If value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.  


Description
Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

Any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:

x = new Boolean(false);
if(x) //the condition is true

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

x = false;
if(x) //the condition is false

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

x = Boolean(expression) //preferred
x = new Boolean(expression) //don't use

If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.

myFalse=new Boolean(false)   // initial value of false
g=new Boolean(myFalse)       //initial value of true
myString=new String("Hello") // string object
s=new Boolean(myString)      //initial value of true

Do not use a Boolean object in place of a Boolean primitive.


Backward Compatibility

JavaScript 1.2 and earlier versions. The Boolean object behaves as follows:

  • When a Boolean object is used as the condition in a conditional test, JavaScript returns the value of the Boolean object. For example, a Boolean object whose value is false is treated as the primitive value false, and a Boolean object whose value is true is treated as the primitive value true in conditional tests. If the Boolean object is a false object, the conditional statement evaluates to false.

  • You can use a Boolean object in place of a Boolean primitive.

Property Summary


Property

Description

constructor

 

Specifies the function that creates an object's prototype.  

prototype

 

Defines a property that is shared by all Boolean objects.  


Method Summary


Method

Description

toSource

 

Returns an object literal representing the specified Boolean object; you can use this value to create a new object. Overrides the Object.toSource method.  

toString

 

Returns a string representing the specified object. Overrides the Object.toString method.  

valueOf

 

Returns the primitive value of a Boolean object. Overrides the Object.valueOf method.  

In addition, this object inherits the watch and unwatch methods from Object.


Examples
The following examples create Boolean objects with an initial value of false:

bNoParam = new Boolean()
bZero = new Boolean(0)
bNull = new Boolean(null)
bEmptyString = new Boolean("")
bfalse = new Boolean(false)

The following examples create Boolean objects with an initial value of true:

btrue = new Boolean(true)
btrueString = new Boolean("true")
bfalseString = new Boolean("false")
bSuLin = new Boolean("Su Lin")


constructor

Specifies the function that creates an object's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name.



Property of  

Boolean  

Implemented in  

JavaScript 1.1, NES 2.0  

ECMA version  

ECMA-262  


Description
See Object.constructor.


prototype

Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, see Function.prototype.



Property of  

Boolean  

Implemented in  

JavaScript 1.1, NES 2.0  

ECMA version  

ECMA-262  


toSource

Returns a string representing the source code of the object.



Method of  

Boolean  

Implemented in  

JavaScript 1.3  


Syntax
toSource()


Parameters
None


Description
The toSource method returns the following values:

  • For the built-in Boolean object, toSource returns the following string indicating that the source code is not available:

       function Boolean() {
          [native code]
       }

  • For instances of Boolean, toSource returns a string representing the source code.
This method is usually called internally by JavaScript and not explicitly in code.


See also
Object.toSource


toString

Returns a string representing the specified Boolean object.



Method of  

Boolean  

Implemented in  

JavaScript 1.1, NES 2.0  

ECMA version  

ECMA-262  


Syntax
toString()


Parameters
None.


Description
The Boolean object overrides the toString method of the Object object; it does not inherit Object.toString. For Boolean objects, the toString method returns a string representation of the object.

JavaScript calls the toString method automatically when a Boolean is to be represented as a text value or when a Boolean is referred to in a string concatenation.

For Boolean objects and values, the built-in toString method returns the string "true" or "false" depending on the value of the boolean object. In the following code, flag.toString returns "true".

var flag = new Boolean(true)
var myVar=flag.toString()


See also
Object.toString


valueOf

Returns the primitive value of a Boolean object.



Method of  

Boolean  

Implemented in  

JavaScript 1.1  

ECMA version  

ECMA-262  


Syntax
valueOf()


Parameters
None


Description
The valueOf method of Boolean returns the primitive value of a Boolean object or literal Boolean as a Boolean data type.

This method is usually called internally by JavaScript and not explicitly in code.


Examples
x = new Boolean();
myVar=x.valueOf()      //assigns false to myVar

 
搞BOA 写CGI遇到的一个问题:【上一篇】
JavaScript对象之数组Array:【下一篇】
【相关文章】
  • javascript加密解密全攻略
  • 关于ASP.NET调用JavaScript的实现
  • Blog技巧, 用JavaScript解决图像不能正常显示问题
  • JavaScript优化总结
  • javascript里的document.all用法
  • 用javascript来显示HTML控件的位置即javascript文件在ASP.NET中的运用
  • 按速度优化JavaScript代码
  • Javascript实现图片放大镜效果
  • JavaScript实现在线编辑表格
  • javascript函数库
  • 【随机文章】
  • 操作符++(后缀)的重载
  • 在SQL Server中使用存储过程的代码
  • 2me Btree
  • 读核日记(七) --linux的内存管理机制(1)
  • C# 程序员参考--数组教程三
  • 墙面4口面板
  • samba的配置[转载]
  • 怎样实现动画背景旗帜
  • DELL的“零式供应链”
  • 光缆技术
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.