js对象
Boolean 对象
Boolean 对象表示两个值:“true” 或 “false”。
在 JavaScript 中,布尔值是一种基本的数据类型。Boolean 对象是一个将布尔值打包的布尔对象。Boolean 对象主要用于提供将布尔值转换成字符串的 toString() 方法。
当调用 toString() 方法将布尔值转换成字符串时(通常是由 JavaScript 隐式地调用),JavaScript 会内在地将这个布尔值转换成一个临时的 Boolean 对象,然后调用这个对象的 toString() 方法。
<script>
// 布尔值有 true false
let flag1=new Boolean(23>9);
console.log(flag1);
</script>
Boolean 对象方法
toSource():返回该对象的源代码。
toString():把逻辑值转换为字符串,并返回结果。
valueOf():返回 Boolean 对象的原始值。
<script>
let flag1=new Boolean(23>9);
console.log("源代码:"+flag1.toSource());
console.log(flag1.toString());
console.log(flag1.valueOf());
</script>
Date 对象
Date 对象用于处理日期和时间。
Date 对象会自动把当前日期和时间保存为其初始值。
<script>
//1.创建对象 当前时间
let now=new Date();
document.write('<h2>'+now+'</h2>');
</script>
Date 对象方法
Date() :返回当日的日期和时间。
getDate() :从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay():从 Date 对象返回一周中的某一天 (0 ~ 6)。
getMonth():从 Date 对象返回月份 (0 ~ 11)。
getFullYear():从 Date 对象以四位数字返回年份。
getHours():返回 Date 对象的小时 (0 ~ 23)。
getMinutes():返回 Date 对象的分钟 (0 ~ 59)。
getSeconds():返回 Date 对象的秒数 (0 ~ 59)。
getMilliseconds():返回 Date 对象的毫秒(0 ~ 999)。
getTime():返回 1970 年 1 月 1 日至今的毫秒数。
getTimezoneOffset():返回本地时间与格林威治标准时间 (GMT) 的分钟差。
Date.parse(‘Jul 02 ?’):指定时间的毫秒数
setFullYear:设置 Date 对象中的年份(四位数字)。
toString():把 Date 对象转换为字符串。
toTimeString():把 Date 对象的时间部分转换为字符串。
<script>
let now=new Date();
document.write('<h2>'+'返回当日的日期和时间:'+Date()+'</h2>');
document.write('<h2>'+'一个月的某一天'+now.getDate()+'</h2>');
document.write('<h2>一周的某一天:'+now.getDay()+'</h2>');
document.write('<h2>月'+now.getMonth()+'</h2>');//只显示0~11月,所以显示后+1才是正确的月份
document.write('<h2>四位数的年:'+now.getFullYear()+'</h2>');
document.write('<h2>时:'+now.getHours()+'</h2>');
document.write('<h2>分'+now.getMinutes()+'</h2>');
document.write('<h2>秒'+now.getSeconds()+'</h2>');
// 2024年7月2日9时1分12秒
document.write('<h2>'+
now.getFullYear()+"年"+
(now.getMonth()+1)+"月"+
now.getDate()+"日"+
now.getHours()+":"+
now.getMinutes()+":"+
now.getSeconds());
document.write('<h2>毫秒'+now.getMilliseconds()+'</h2>');
document.write('<h2>1970年1月1日至今的毫秒数'+now.getTime()+'</h2>');
document.write('<h2>格林威治时间和本地时间之间的时差:'+now.getTimezoneOffset()+'</h2>');
document.write('<h2>指定时间的毫秒数:'+Date.parse('Jul 02 2024')+'</h2>');
document.write('<h2>---------------------------------------</h2>');
// 设置时间
now.setFullYear(2025);
document.write('<h2> Date 对象转换为字符串'+now.toString()+'</h2>');
document.write('<h2>时间部分转为字符串'+now.toTimeString()+'</h2>');
document.write('<h2>'+now+'</h2>');
document.write('<h2>'+now+'</h2>');
document.write('<h2>'+now+'</h2>');
document.write('<h2>'+now+'</h2>');
</script>
定时器:
upDaye();
1000毫秒后执行一次upDate 函数 每隔1000毫秒执行一次
设置当前时间
<h2>
<span class="y">2024</span>年
<span class="m">7</span>月
<span class="d">2</span>日
<span class="h">7</span>:
<span class="f">37</span>:
<span class="s">20</span>
</h2>
<script>
let y=document.querySelector(".y");
let m=document.querySelector(".m");
let d=document.querySelector(".d");
let h=document.querySelector(".h");
let f=document.querySelector(".f");
let s=document.querySelector(".s");
// 修改时间
function upDaye(){
let now=new Date();
y.innerHTML=now.getFullYear();
m.innerHTML=now.getMonth()+1;
d.innerHTML=now.getDate();
h.innerHTML=now.getHours();
f.innerHTML=now.getMinutes();
s.innerHTML=now.getSeconds();
}
let iv=setInterval(upDaye,1000);
</script>
Math 对象
Math 对象用于执行数学任务。
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(),像 Math.sin() 这样的函数只是函数,不是某个对象的方法。您无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。
Math 对象属性
E:返回算术常量 e,即自然对数的底数(约等于2.718)。
PI:返回圆周率(约等于3.14159)。
<script>
console.log("自然底数e"+Math.E);
console.log("圆周率:"+Math.PI);
</script>
Math 对象方法
abs(x):返回数的绝对值。
acos(x):返回数的反余弦值。
atan(x):以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x):返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
cos(x):返回数的余弦。
sin(x):返回数的正弦。
tan(x):返回角的正切。
ceil(x):对数进行上舍入。
floor(x):对数进行下舍入。
round(x):把数四舍五入为最接近的整数。
exp(x):返回 e 的指数。
log(x):返回数的自然对数(底为e)。
max(x,y):返回 x 和 y 中的最高值。
min(x,y):返回 x 和 y 中的最低值。
pow(x,y):返回 x 的 y 次幂。
random():返回 0 ~ 1 之间的随机数。
valueOf():返回 Math 对象的原始值。
<script>
console.log("返回数的绝对值:"+Math.abs(1));
console.log("返回数的反余弦值:"+Math.acos(0.9));
console.log("反正切值:"+Math.atan(2));
console.log("返回从x轴到点(x,y)的角度"+Math.atan2(10,5));
console.log("余弦:"+Math.cos(0.9));
console.log("正弦:"+Math.sin(0.5));
console.log("正切:"+Math.tan(9));
console.log("上舍入:"+Math.ceil(123.1));
console.log("下舍入:"+Math.floor(123.9));
console.log("四舍五入"+Math.round(123.4));//ji
console.log("返回e的指数"+Math.exp(123));
console.log("e的自然对数:"+Math.log(123));
console.log("最大值:"+Math.max(12,15));//ji
console.log("最小值:"+Math.min(12,18));//ji
console.log("x的y次幂:"+Math.pow(2,6));//ji
console.log("0~1之间的随机数"+Math.random());//ji
console.log("原始值:"+Math.valueOf());
</script>
Number 对象
Number 对象是原始数值的包装对象。
当 Number() 和运算符 new 一起作为构造函数使用时,它返回一个新创建的 Number 对象。如果不用 new 运算符,把 Number() 作为一个函数来调用,它将把自己的参数转换成一个原始的数值,并且返回这个值(如果转换失败,则返回 NaN)。
Number 对象属性
MAX_VALUE:可表示的最大的数。
MIN_VALUE:可表示的最小的数。
NEGATIVE_INFINITY:负无穷大,溢出时返回该值。
POSITIVE_INFINITY:正无穷大,溢出时返回该值。
<script>
console.log("数字的最大值:"+Number.MAX_VALUE);
console.log("数字的最小值:"+Number.MIN_VALUE);
console.log("负无穷大:"+Number.NEGATIVE_INFINITY);
console.log("正无穷大:"+Number.POSITIVE_INFINITY);
</script>
Number 对象方法
toString:把数字转换为字符串,使用指定的基数。(toString(2):2进制 toString(8):8进制 toString(16):16进制 toString(36):36进制)
toLocaleString:把数字转换为字符串,使用本地数字格式顺序。
toFixed:把数字转换为字符串,结果的小数点后有指定位数的数字。
toExponential:把对象的值转换为指数计数法。
valueOf:返回一个 Number 对象的基本数字值。(数字转变字符串)
<script>
// 声明一个数字
let a=1123;
let b=Number(1123);
let c=new Number(2345);
console.log("2进制:"+b.toString(2));
console.log("8进制:"+b.toString(8));
console.log("16进制:"+b.toString(16));
console.log("36进制:"+b.toString(36));
console.log("本地转换:"+b.toLocaleString());
// Number 四舍五入为指定小数位数的数字
console.log("指定小数:"+c.toFixed(2));//要记住的
console.log("指数:"+c.toExponential(1));
console.log("默认值:"+c.valueOf());//数字转变字符串
</script>