2021-03-11——JavaScript_6

JavaScript_6

1.字符串对象(String

2.Number 对象

3.JavaScript Array(数组)

4.JavaScript Boolean(布尔)【注意Boolean对象与boolean值的差异】

  1.创建boolena对象

1.1直接赋值

Var  boo1=false;

1.2通过构造器创建boolean对象

通过构造器创建boolean对象可以将

数字0--false   非0--true

字符串 “”---false  非””--true

null---false

NaN---false

对象【Boolean对象】-- true

例如:

var b1=new Boolean(0);

                          var b2=new Boolean(-1);

                          var b3=new Boolean("");

                          var b4=new Boolean(null);

                          var b5=new Boolean(NaN);

                          var b6=new Boolean("false");

                         

                          var b11= Boolean(0);

                          var b22= Boolean(-1);

                          var b33= Boolean("");

                          var b44= Boolean(null);

                          var b55= Boolean(NaN);

                          var b66= Boolean("false");



                          document.write("<h1>0 为布尔值 "+ b1 +"Boolean对象</h1>");

                          document.write("<h1>1 为布尔值 "+ b2 +"Boolean对象</h1>");

                          document.write("<h1>空字符串是布尔值 "+ b3 + "Boolean对象</h1>");

                          document.write("<h1>null 是布尔值 "+ b4+ "Boolean对象</h1>");

                          document.write("<h1>NaN 是布尔值 "+ b5 +"Boolean对象</h1>");

                          document.write("<h1>字符串'false' 是布尔值"+ b6 +"Boolean对象</h1>");

                         

                          document.write("<h1>0 为布尔值 "+ b11 +"boolean值</h1>");

                          document.write("<h1>1 为布尔值 "+ b22 +"boolean值</h1>");

                          document.write("<h1>空字符串是布尔值 "+ b33 + "boolean值</h1>");

                          document.write("<h1>null 是布尔值 "+ b44+ "boolean值</h1>");

                          document.write("<h1>NaN 是布尔值 "+ b55 +"boolean值</h1>");

                          document.write("<h1>字符串'false' 是布尔值"+ b66 +"boolean值</h1>");

valueOf() 返回 Boolean 对象的原始值。boolean数据值

注意:

1.创建Boolena对象是通过new Boolean()的方式得到的是Boolean对象,通过valueOf()得到的是boolean的原始值。

2.无论是不是Boolen对象,只要是对象,在if语句的判断条件中会被转换成boolean值且一定是true。通过Boolen对象提供的valueOf()方法得到Boolen对象中的boolean数据值。

5.JavaScript Date(日期)

1、创建Date对象

new Date(); //当前系统时间

例如:得到当前系统时间  

var date=new Date();

document.write(date);

Thu Nov 19 2020 11:01:14 GMT+0800 (中国标准时间)
         2.new Date(milliseconds) //返回从 1970 年 1 月 1 日至今的毫秒数

例如:

var date2=new Date(10000);

document.write("<h1>"+date2+"</h1>");

3.new Date(dateString)

例如:

var date3=new Date("2018/12/1 12:29:30");

document.write("<h1>"+date3+"</h1>");


         4.new Date(year, month, day, hours, minutes, seconds, milliseconds)

例如:

 

  1. Date的常用方法

getTime() 返回从 1970 年 1 月 1 日至今的毫秒数。

    getFullYear()获取年份

getMonth()获取月份【从0开始数,我们在使用的是需要+1】

getDate()获取月中的天数

getDay()获取星期

getHours()获取小时数

getMinutes()获取分钟数

getSeconds()获取秒数

setFullYear(y,m,d) 设置具体的日期。

setMonth()设置月份【从0开始数,我们在使用的是需要-1】

setDate()设置月中的天数

setDay()设置星期

setHours()设置小时数

setMinutes()设置分钟数

setSeconds()设置秒数

setTime()设置毫秒数,得到从 1970 年 1 月 1 日设置的毫秒数重新给出时间日期

例如:

//getTime() 返回从 1970 年 1 月 1 日至今的毫秒数。                       

   document.write("<h1>"+date.getTime()+"</h1>");

                          //getFullYear()获取年份

                          document.write("<h1>"+date.getFullYear()+"</h1>");

                          //getMonth()获取月份【从0开始数,我们在使用的是需要+1】

                          document.write("<h1>"+(date.getMonth()+1)+"</h1>");

                          //getDate()获取月中的天数

                          document.write("<h1>"+date.getDate()+"</h1>");

                          //getDay()获取星期

                          document.write("<h1>"+date.getDay()+"</h1>");

                          //getHours()获取小时数

                          document.write("<h1>"+date.getHours()+"</h1>");

                          //getMinutes()获取分钟数

                          document.write("<h1>"+date.getMinutes()+"</h1>");

                          //getSeconds()获取秒数

                          document.write("<h1>"+date.getSeconds()+"</h1>");

                          //setFullYear() 设置具体的日期。

                          //【月份从0开始数,我们在设置的是需要-1】

                          //date.setFullYear(2019,10,20); []

                          //date.setHours(12);

                          //date.setMinutes(12);

                          //date.setSeconds(12);

                          //setTime设置毫秒数,

                          //得到从 1970 年 1 月 1 日设置的毫秒数重新给出时间日期

                          date.setTime(10000)

                          document.write("<h1>"+date+"</h1>");

如何在网页上显示一个钟表。

<script>

function startTime(){

         var today=new Date();

         var h=today.getHours();

         var m=today.getMinutes();

         var s=today.getSeconds();// 在小于10的数字钱前加一个‘0’

         m=checkTime(m);

         s=checkTime(s);

         document.getElementById('txt').innerHTML=h+":"+m+":"+s;

         t=setTimeout(function(){startTime()},500);

}

function checkTime(i){

         if (i<10){

                  i="0" + i;

         }

         return i;

}

</script>

两个日期比较

日期对象也可用于比较两个日期。

下面的代码将当前日期与 2100 年 1 月 14 日做了比较:

var x=new Date();
x.setFullYear(2100,0,14);//设置一个时间用来比较
var today = new Date(); //得到当天的时间日期
if (x>today){
alert("今天是2100年1月14日之前");
}else{
alert("今天是2100年1月14日之后");
}
  1. JavaScript Math(算数)

Math(算数)对象的作用是:执行常见的算数任务。

算数值【常量值】

Math.E  --  自然常数,为数学中一个常数,是一个无限不循环小数,且为超越数,其值约为2.718281828459045。
Math.PI------圆周率

算数方法

max()返回两个给定的数中的较大的数

min()返回两个给定的数中的较小的数

random()返回 0 到 1 之间的随机数。

round()最为接近的整数,注意负数

例如:

document.write("<h1>自然常数=="+Math.E+"</h1>");

document.write("<h1>圆周率=="+Math.PI+"</h1>");

document.write("<h1>平方根=="+Math.sqrt(9)+"</h1>");

document.write("<h1>立方根=="+Math.cbrt(8)+"</h1>");

document.write("<h1>次幂=="+Math.pow(2,3)+"</h1>");

document.write("<h1>随机数=="+Math.random()+"</h1>");

document.write("<h1>最为接近的整数=="+Math.round(-12.6)+"</h1>");

document.write("<h1>最大数=="+Math.max(12,34,8)+"</h1>");

document.write("<h1>最小数=="+Math.min(12,34,8)+"</h1>");

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值