四、JavaScript Boolean(布尔)【注意Boolean对象与boolean值的差异】
一、创建boolena对象
1直接赋值
Var boo1=false;
2通过构造器创建boolean对象
通过构造器创建boolean对象可以将
数字0–false 非0–true
字符串 “”—false 非””–true
null—false
NaN—false
对象【Boolean对象】-- true
字符串:空字符串是false,非空为true
<script>
//直接赋值
var boo1=true;
var boo2=false;
//通过构造器创建Boolean对象
//通过Boolean构造器可以把字符串的true或false转变成布尔型对象
var boo3 = new Boolean("年轻人 耗子尾汁!");
if(boo3){
alert("boolean对象");
}
</script>
<title>Document</title>
<script>
//直接赋值
//var boo1=true;
var boo1=false;
//通过构造器创建Boolean对象
//通过Boolean构造器可以把字符串的true或false转变成布尔型对象
/* var boo3 = new Boolean("年轻人 耗子尾汁!");
if(boo3){
alert("boolean对象");
} */
//var boo2=new Boolean("libo haoziweizhi");
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>");
//var num2=100;
//num1 = null;
//var bootest=new Boolean(num1);
// testboo--Boolean对象
//无论数不是Boolen对象,主要是对象,在if语句的判断条件中
//会被转换成boolean值一定是true
//通过过Boolean对象提供的方法
//valueOf() 返回 Boolean 对象的原始值。
//var te=testboo.valueOf();
//if(bootest){
// alert(num1.toFixed(2));}
</script>
注意:
1.创建Boolena对象是通过new Boolean()的方式得到的是Boolean对象,通过Boolean()得到的是boolean的原始值。
2.无论是不是Boolen对象,只要是对象,在if语句的判断条件中会被转换成boolean值且一定是true。通过Boolen对象提供的valueOf()方法得到Boolen对象中的boolean数据值。
valueOf() 返回 Boolean 对象的原始值。boolean数据值
五、JavaScript Date(日期)
1、创建Date对象
new Date(); //当前系统时间
<script>
//1.创建Date对象
var date = new Date();
alert(date);
</script>
<script>
//1.创建Date对象
var date = new Date();
document.write(date);
//alert(date);
</script>
2.Date的常用方法
getTime() 返回从1970年1月1日至今的毫秒数。
<script>
//1.创建Date对象
var date = new Date();
document.write("<h1>"+date+"</h1>");
//alert(date);
//getTime()返回从1970年1月1日至今的毫秒数。
document.write("<h1>" +date.getTime()+ "</h1>");
</script>
getFullYear()使用getFullYear()获取年份
<script>
//1.创建Date对象
var date = new Date();
document.write("<h1>"+date+"</h1>");
//alert(date);
//getTime()返回从1970年1月1日至今的毫秒数。
document.write("<h1>" +date.getTime()+ "</h1>");
//getFullYear()获取年份
document.write("<h1>" +date.getFullYear()+ "</h1>");
</script>
getMonth() 获取月份 从零开始 我们在使用时需要加一
<script>
//1.创建Date对象
var date = new Date();
document.write("<h1>"+date+"</h1>");
//alert(date);
//getTime()返回从1970年1月1日至今的毫秒数。
document.write("<h1>" +date.getTime()+ "</h1>");
//getFullYear()获取年份
document.write("<h1>" +date.getFullYear()+ "</h1>");
//getMonth() 获取月份 从零开始 我们在使用时需要加一
document.write("<h1>" +(date.getMonth()+1)+ "</h1>");
</script>
getDate()获取月中的天数
<script>
//1.创建Date对象
var date = new Date();
document.write("<h1>"+date+"</h1>");
//alert(date);
//getTime()返回从1970年1月1日至今的毫秒数。
document.write("<h1>" +date.getTime()+ "</h1>");
//getFullYear()获取年份
document.write("<h1>" +date.getFullYear()+ "</h1>");
//getMonth() 获取月份 从零开始 我们在使用时需要加一
document.write("<h1>" +(date.getMonth()+1)+ "</h1>");
//getDate()获取月中的天数
document.write("<h1>" +date.getDate()+ "</h1>");
//得到星期
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>");
</script>
setFullYear(y,m,d) 设置具体的日期
setMonth()设置月份【从0开始数,我们在使用的是需要-1】
setDate()设置月中的天数
setDay()设置星期
setHours()设置小时数
setMinutes()设置分钟数
setSeconds()设置秒数
setTime()设置毫秒数,得到从 1970 年 1 月 1 日设置的毫秒数重新给出时间日期
<script>
//1.创建Date对象
var date = new Date();
document.write("<h1>"+date+"</h1>");
//alert(date);
//getTime()返回从1970年1月1日至今的毫秒数。
document.write("<h1>" +date.getTime()+ "</h1>");
//getFullYear()获取年份
document.write("<h1>" +date.getFullYear()+ "</h1>");
//getMonth() 获取月份 从零开始 我们在使用时需要加一
document.write("<h1>" +(date.getMonth()+1)+ "</h1>");
//getDate()获取月中的天数
document.write("<h1>" +date.getDate()+ "</h1>");
//得到星期
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>");
//月份从0开始,我们在使用的时候需要减一
//date.setFullYear(2018,7,20);
//setHours()设置小时数
/* date.setHours(10);
//setMinutes()设置分钟数
setMinutes(10);
//setSeconds()设置秒数
setSeconds(10); */
//setTime设置毫秒数,
date.setTime(10000);
document.write("<h1>" +date+ "</h1>");
</script>
6.JavaScript Math(算数)
Math(算数)对象的作用是:执行常见的算数任务。
算数值【常量值】
Math.E – 自然常数,为数学中一个常数,是一个无限不循环小数,且为超越数,其值约为2.718281828459045。
Math.PI------圆周率
<script>
document.write("<h1>自然常数==" +Math.E+"</h1>");
document.write("<h1>圆周率π==" +Math.PI+"</h1>");
</script>
算数方法
max()返回两个给定的数中的较大的数
min()返回两个给定的数中的较小的数
random()返回 0 到 1 之间的随机数。
round()最为接近的整数
<title>Document</title>
<script>
document.write("<h1>自然常数==" +Math.E+"</h1>");
document.write("<h1>圆周率π==" +Math.PI+"</h1>");
document.write("<h1>绝对值==" +Math.abs(-10)+"</h1>");
document.write("<h1>平方根==" +Math.sqrt(9)+"</h1>");
document.write("<h1>立方根==" +Math.cbrt(27)+"</h1>");
document.write("<h1>次幂==" +Math.pow(2,7)+"</h1>");
document.write("<h1>随机数==" +Math.random()+"</h1>");
document.write("<h1>最为接近的整数==" +Math.round(-15.5)+"</h1>");
</script>