学习(十三)ES6 - Boolean

布尔对象表示两个值,“ true”或“ false”。如果value参数省略或为0,-0,null,false,NaN,undefined或空字符串(“”),则对象的初始值为false。

语法:var val = new Boolean(value);

 Boolean属性

Sr.No属性和说明
1constructor

返回对创建对象的布尔函数的引用。

2prototype

prototype属性可以向对象添加属性和方法。

Javascript Boolean constructor()方法返回对创建实例原型的布尔函数的引用。

语法:boolean.constructor() 它返回创建该对象实例的函数。

 

<html>
   <head>
      <title>JavaScript constructor() Method</title>
   </head>
   <body>
      <script type="text/javascript">
         var bool = new Boolean( );
         document.write("bool.constructor() is : " + bool.constructor); 
        //输出 bool.constructor() is : function Boolean() { [native code] }
      </script>
   </body>
</html>

prototype属性允许您向任何对象(Number,Boolean,String和Date等)添加属性和方法。

 –原型是全局属性,几乎所有对象都可以使用。

 语法:object.prototype.name = value

<html>
   <head>
      <title>User-defined objects</title>
      <script type="text/javascript">
         function book(title, author){
            this.title = title;
            this.author = author;
         }
      </script>
   </head>
   <body>
      <script type="text/javascript">
         var myBook = new book("Perl", "Tom");
         book.prototype.price = null;
         myBook.price = 100;
         document.write("Book title is : " + myBook.title + "<br>");//输出:Book title is : Perl
         document.write("Book author is : " + myBook.author + "<br>");//输出:Book author is : Tom
         document.write("Book price is : " + myBook.price + "<br>");//输出:Book price is : 100
      </script>
   </body>
</html>

Boolean方法

Sr.No方法和说明
1 toSource()

返回一个字符串,其中包含布尔对象的源;您可以使用此字符串创建等效对象。

2 toString()

根据对象的值返回“ true”或“ false”的字符串。

3valueOf()

返回布尔对象的原始值。

java语言的boolean toSource()方法返回一个表示对象源代码的字符串。

注意 -此方法并非与所有浏览器兼容。

<html>
   <head>
      <title>JavaScript toSource() Method</title>
   </head>
   <body>
      <script type="text/javascript">
         function book(title, publisher, price) {
            this.title = title;
            this.publisher = publisher;
            this.price = price;
         }
         var newBook = new book("Perl","Leo Inc",200);
         document.write("newBook.toSource() is : "+ newBook.toSource());
         //输出:({title:"Perl", publisher:"Leo Inc", price:200})
      </script>
   </body>
</html>

Boolean toSource()方法根据对象的值返回“ true”或“ false”的字符串。

<html>
   <head>
      <title>JavaScript toString() Method</title>
   </head>
   <body>
      <script type="text/javascript">
         var flag = new Boolean(false);
         document.write( "flag.toString is : " + flag.toString());//输出:flag.toString is : false
      </script>
   </body>
</html>

Javascript Boolean valueOf()方法返回指定布尔对象的原始值。 

<html>
   <head>
      <title>JavaScript toString() Method</title>
   </head>
   <body>
      <script type="text/javascript">
         var flag = new Boolean(false);
         document.write( "flag.valueOf is : " + flag.valueOf()); //输出:flag.valueOf is : false
      </script>
   </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值