**Number对象
1、Number对象是原始数值的包装对象
2、创建:当参数值不为数字时,返回NaN。
①字面量
var num=10;
②new 关键字 构造函数
var num2=new Number(100); console.log(num2); var num3=new Number("hello"); console.log(num3); //NaN not a number
3、精度,进制:JS中 只用一种数字类型,包含整数和小数(浮点型)
var num1=100;
var num2=100.0;
科学计数法: 极大的成绩效的数字可以使用科学(指数)计数法表示 e:10的次方/次幂
var a=12300000000000;
var b=123e11;
console.log(a); //12300000000000
console.log(b); //12300000000000
console.log(a===b); //true
var c=0.00000000000123;
var d=1.23e-12;
console.log(c); //1.23e-12
console.log(d); //1.23e-12
-精度
整数(不适用于小数点或科学计数法)最多15位
var x=999999999999999;
var y=9999999999999999;
console.log(x); //999999999999999
console.log(y); //10000000000000000
小数 最大位数17位 运算不能保证100%准确性
var n=0.0000000000000001;
var m=0.00000000000000012;
console.log(n); //1e-16
console.log(m); //1.2e-16
console.log(0.1+0.2); //0.300000000000000004
//十进制转换为二进制 二进制转换为十进制 出现了偏差
-进制
十进制 逢十进一 默认
十六进制 逢十六进一 前缀0x 01233456789abcdef
var j=0xaa; //10*1+10*16
console.log(j); //170
八进制 逢八进一 前缀0
var k=0211;//1*1+1*8+2*8*8
consloe.log(k); //137
二进制 逢二进一
进制的转换 toString(进制)
var m=20;
console.log(m.toString(8)); //24
console.log(m.toString(16)); //14
console.log(m.toString(2))); //10100
console.log(m.toString(10))); //170
4、Number对象的属性
constructor 返回对创建此对象的Number函数的引用。
prototype 允许你可以向对象添加属性和方法
MAX_VALUE 可表示的最大数。
console.log(Number.MAX_VALUE); //1.7976931348623157e+308
MIN_VALUE 可表示的最小的数。
console.log(Number.MIN_VALUE); //5e-324
NEGATIVE_INFINITY 负无穷大,溢出时返回该值。
console.log(Number.NEGATIVE_INFINITY); //-Infinity
console.log(-2/0); //-Infinity
POSITIVE_INFINITY 正无穷大,溢出时返回该值。
console.log(Number.POSITINV_INFINITY); //Infinity
console.log(2/0); //Infinity
console.log(2/0===Number.POSITINV_INFINITY); //true
NaN 非数字值
var a=NaN;
var b=NaN;
console.log(a===b); //false
Es6新增
MAX_SAFE_INTEGER 表示在JavaScript中最大的安全整数(253-1)。 //9007199254720991
MIN_SAFE_INTEGER 表示在JavaScript中最小的安全整数(253-1)。 //-9007199254720991
EPSILOW 表示1和比接近1且大于1 的最小Number之间的差距。
5、Number对象的方法
toExponential() 把对象的值转换为只数(科学)计数法
console.log((123.000000000).toExponential()); //1.230000000001e+2
toFixed() 把数字转换为字符串,结果的小数点后有指定位数的数字。
console.log((123.25235252352).toFixed(2)); //123.25 遵循舍四进五
toPrecision() 把数字格式化为指定的长度 包括小数点前的数字,不用四舍五入
console.log((123.25235252352).toPrecision(5)); //123.25
isFinite(参数) 检测指定参数是否不为无穷大。参数为NaN或是正负无穷大返回false其他返回true。
console.log(Number.isFinite(123)); //true
isNaN(参数) 检测一个参数是否不是一个数字。
isInteger(参数) 检测一个参数是否为整数。
isSafeInteger(参数) 检测一个参数是否为安全整数。
**Boolean对象
Boolean对象用于将非布尔值转换为布尔值(true或false)
var a=true;
var b=false;
// var c=new Boolean(参数) 参数可以非布尔值 自动转换
var c=new Boolean("");
console.log(c)
属性
constructor 返回对创建此对象的 Boolean 函数的引用
prototype 给对象添加属性和方法 原型
方法
toString() 把布尔值转换为字符串
console.log(c.toString());
valueOf() 返回 Boolean 对象的原始值。
console.log(c.valueOf()); //false
**Math对象
属性:
Math:用于处理数学任务 内联(建)样式 console.log(Math);
1、E: 返回算术常量 e,即自然对数的底数(约等于2.718)。console.log(Math.E);//2.718281828459045
2、LN2: 返回 2 的自然对数(约等于0.693)。 以E为底,2的对数console.log(Math.LN2);//0.6931471805599453
3、LN10: 10 的自然对数(约等于2.302)。
console.log(Math.LN10);//2.302585092994046
4、LOG2E: 返回以 2 为底的 e 的对数(约等于 1.4426950408889634)。console.log(Math.LOG2E);//1.4426950408889634
5、LOG10E: 返回以 10 为底的 e 的对数(约等于0.434)。console.log(Math.LOG10E);//0.4342944819032518
6、PI: 返回圆周率(约等于3.14159)。
console.log(Math.PI);//3.141592653589793
7、SQRT1_2: 返回 2 的平方根的倒数(约等于 0.707)。console.log(Math.SQRT1_2);//0.7071067811865476
8、SQRT2: 返回 2 的平方根(约等于 1.414)。console.log(Math.SQRT2);//1.4142135623730951
9、Math.log(x) 返回数的自然数对数(底为e)
console.log(Math.log(Math.E));//1
在数学中,对数是对求幂的逆运算
E: 2.718281828459045
LN2: 0.6931471805599453
LN10: 2.302585092994046
LOG2E: 1.4426950408889634
LOG10E: 0.4342944819032518
PI: 3.141592653589793
SQRT1_2: 0.7071067811865476
SQRT2: 1.4142135623730951
方法
1.三角函数 正弦 余弦 正切 ...
sin(x) 返回数的正弦。 对边/斜边
console.log(Math.sin(Math.PI/6));//360度 = 2Π弧度 1/2
cos(x) 返回数的余弦。 临边/斜边
console.log(Math.cos(Math.PI/3));
tan(x) 返回角的正切。 对边/临边
console.log(Math.tan(Math.PI/4));
asin(x) 返回 x 的反正弦值。
console.log(Math.asin(0.5));//返回弧度
acos(x) 返回 x 的反余弦值。
atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
2.对数
log(x) 返回数的自然对数(底为e)。
console.log(Math.log(Math.E));//1
3. 幂 平方根
sqrt() 返回数的平方根。
console.log(Math.sqrt(2));//1.4142135623730951 console.log(Math.sqrt(3));//1.7320508075688772 console.log(Math.sqrt(5));//2.23606797749979
pow(x,y) 返回 x 的 y 次幂。
console.log(Math.pow(2,2));//4 console.log(Math.pow(3,3));//27
exp(x) 返回 Ex 的指数。 返回E的x次方
console.log(Math.exp(3)); console.log(Math.pow(2.718281828459045,2))
4.最大 最小值
max(x,y,z,...,n) 返回 x,y,z,...,n 中的最高值。
console.log(Math.max(1,2,3,4,5,6,7));
min(x,y,z,...,n) 返回 x,y,z,...,n中的最低值。
console.log(Math.min(1,2,3,4,5,6,7));
5.取值 绝对值 四舍五入 向上取整 向下取整 随机数
abs(x) 返回 x 的绝对值。
console.log(Math.abs(-3.222)); //3.222 console.log(Math.abs(3.4444));
ceil() 对数进行上舍入 向上取整
console.log(Math.ceil(1.1));//2
floor() 对数进行下舍入 向下取整
console.log(Math.floor(1.9));//1
round() 四舍五入
console.log(Math.round(10.5));//11 console.log(Math.round(10.4));//10 console.log(Math.round(-10.6));//-10
random() 返回 0 ~ 1 之间的随机数。 包含0 不包含1
console.log(Math.random());
取随机整数 范围时300-1000
console.log(Math.round(Math.random()*10));//0到10 console.log(Math.round(Math.random()*700));//0到700 300-1000 console.log(Math.round(Math.random()*700)+300);//300到1000
**Date对象
**Date对象的构建:
使用new 关键字 Date() 构造函数
1、获取当前时间
var oDate=new Date();
console.log(oDate); //无参数时 获取当前日期时间
2、自定义时间
new Date(year,month,day,hour,minute,second,millisecond)
参数为纯数字时 表示为毫秒数 返回距离1970年1月1日0点的毫秒数
var oDate2=new Date(100000);//
console.log(oDate2);//Thu Jan 01 1970 08:01:40 GMT+0800 (中国标准时间)
//计算机中将1970年1月1日0时0份0秒作为时间纪元。
将年月日作为参数传进去
var oDate3=new Date(2022,1,1);
console.log(oDate3);
时分秒
var oDate4=new Date(2022,3,29,18,0,0);
console.log(oDate4);
时间格式 字符串作为参数
var oDate5=new Date("2022-4-29,18:00:00");
var oDate6=new Date("2022/4/29,18:00:00");
console.log(oDate5);
console.log(oDate6);
**Date对象的属性
constructor 返回对创建此对象的 Date 函数的引用
prototype 向对象中添加属性和方法 原型
Date.prototype.name="日期时间对象"
console.log(new Date().name)
**Date对象的方法1
var oDate1=new Date();
*getFullYear 从Date对象以四位数字返回年份
console.log(oDate1.getFullYear()); //2022
***getMonth 从Date对象返回月份(0~11)。1到12月份
console.log(oDate1.getMonth());//3
*getDate() 从Date对象返回一个月中的某一天 (1 ~ 31)。
console.log(oDate1.getDate());//21
**getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。周日-周六
console.log(oDate1.getDay());//4
*getHours() 返回 Date 对象的小时 (0 ~ 23)。
console.log(oDate1.getHours());//19
*getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
console.log(oDate1.getMinutes());
*getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
console.log(oDate1.getSeconds());//43
*getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
console.log(oDate1.getMilliseconds());
***getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
console.log(oDate1.getTime());
var oDate2=new Date(100000);
console.log(oDate2.getTime());
**getTimezoneOffset() 返回本地时间与格林威治标准时间(GMT)的分钟差。
console.log(oDate1.getTimezoneOffset()); //-480
格林威治时间 与本地时间大致相差8小时。
getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。
getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。
getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。
getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。
console.log(oDate1.getUTCHours());//9
getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。
getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。
getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。
getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。
getYear() 已废弃。 请使用 getFullYear() 方法代替。
设置
setFullYear() 设置 Date 对象中的年份(四位数字)。
setMonth() 设置 Date 对象中月份 (0 ~ 11)。
setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
setHours() 设置 Date 对象中的小时 (0 ~ 23)。
setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
var oDate3=new Date(); console.log(oDate3); oDate3.setFullYear(2024); console.log(oDate3); //设置为2024年当前时间段 oDate3.setMonth(1); console.log(oDate3); //设置为2月份当前时间段
setTime() 以毫秒设置Date对象,1970年1月1日至今的毫秒数。
var oDate3=new Date(); console.log(oDate3); oDate3.setTime(1000); console.log(oDate3);
设置世界时间 格林威治时间
setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。
setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。
setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。
setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。
setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。
setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。
setUTCSeconds() setUTCSeconds() 方法用于根据世界时 (UTC)
**Date对象的方法2
var oDate1=new Date();
**toString() 把Date对象转换成字符串
console.log(oDate1.toString());
**toDateString() 把Date对象的日期部分转换为字符串。
console.log(oDate1.toDateString());//Fri Apr 22 2022
**toTimeString() 把Date对象的时间部分转换为字符串。
console.log(oDate1.toTimeString());//10:48:19 GMT+0800 (中国标准时间)
**toLocaleString() 根据本地时间格式,把Date对象转换为字符串。
console.log(oDate1.toLocaleString());//2022/4/22 上午10:50:31
**toLocaleDateString() 根据本地时间格式,把Date对象的日期部分转换为字符串。
console.log(oDate1.toLocaleDateString());
**toLocaleTimeString() 根据本地时间格式,把Date对象的时间部分转换为字符串。
console.log(oDate1.toLocaleTimeString());
**toUTCString() 根据世界时间,把Date对象转换为字符串
console.log(oDate1.toUTCString());//Fri, 22 Apr 2022 02:53:01 GMT
**toISOString() 使用ISO标准返回字符串的日期格式。
console.log(oDate1.toISOString());//2022-04-22T02:54:43.953Z
**toJSON() 以 JSON 数据格式返回日期字符串。
console.log(oDate1.toJSON());//2022-04-22T02:57:02.910Z
**valueOf() 返回Date对象的原始值
console.log(oDate1.valueOf());//1650596386824
console.log(new Date(1650596386824));
**UTC() 根据世界时间返回1970年1月1日到指定日期的毫秒数
console.log(Date.UTC(1970,0,2));//86400000
**parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数
console.log(Date.parse("1970,1,2"));//57600000