0
点赞
收藏
分享

微信扫一扫

JavaScript的第十天

东林梁 2022-04-21 阅读 93

Boolean

Boolean对象用于将非布尔值转换为布尔值(true或false)

var a=true;
    var b=false;
    // var c=new Boolean(参数)  参数可以非布尔值  自动转换
    var c=new Boolean("");
    console.log(c)

属性

console.log(Boolean.prototype)

constructor 返回对创建此对象的 Boolean 函数的引用

prototype 给对象添加属性和方法 原型

方法

// toString() 把布尔值转换为字符串

console.log(c.toString());
    // valueOf() 返回 Boolean 对象的原始值。
    console.log(c.valueOf())

JavaScript的Date对象

使用new 关键字 Date() 构造函数

 var oDate=new Date(参数)

1.获取当前日期 时间

var oDate1=new Date();//无参数时  获取当前日期时间
    console.log(oDate1);

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);

JavaScript的Date对象的属性

使用new 关键字 Date() 构造函数

var oDate=new Date(参数)

1.获取当前日期 时间

var oDate1=new Date();//无参数时

获取当前日期时间

console.log(oDate1);
    console.log(oDate1.name);
    console.log(Date.prototype);

constructor 返回对创建此对象的 Date 函数的引用

// prototype 向对象中添加属性和方法 原型

Date.prototype.name="日期时间对象"
    console.log(new Date().name)

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);

1.获取当前日期 时间

var oDate1=new Date();
    console.log(oDate1);
    console.log(Date.prototype);

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());//17
    // 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());

格林威治时间

// 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)。

// setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。

// setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。

// 设置世界时间 格林威治时间

// setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。

// setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。

// setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。

// setUTCSeconds() setUTCSeconds() 方法用于根据世界时 (UTC)

JavaScript的Math对象的属性

Math:用于处理数学任务

console.log(Math);

E: 返回算术常量 e,即自然对数的底数(约等于2.718)。

console.log(Math.E);//2.718281828459045

LN2: 返回 2 的自然对数(约等于0.693)。 以E为底,2的对数

console.log(Math.LN2);//0.6931471805599453

LN10: 10 的自然对数(约等于2.302)。

console.log(Math.LN10);//2.302585092994046

LOG2E: 返回以 2 为底的 e 的对数(约等于 1.4426950408889634)。

console.log(Math.LOG2E);//1.4426950408889634

LOG10E: 返回以 10 为底的 e 的对数(约等于0.434)。

console.log(Math.LOG10E);//0.4342944819032518

PI: 返回圆周率(约等于3.14159)。

console.log(Math.PI);//3.141592653589793

SQRT1_2:返回2的平方根的倒数(约等于 0.707)。

console.log(Math.SQRT1_2);//0.7071067811865476

SQRT2: 返回 2 的平方根(约等于 1.414)。

console.log(Math.SQRT2);//1.4142135623730951
// 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

JavaScript的Math对象的方法

Math:用于处理数学任务

console.log(Math);

1.三角函数 正弦 余弦 正切 ...

sin(x) 返回数的正弦。 对边/斜边

console.log(Math.sin(Math.PI/6));//360度 = 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));
    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

01Number对象的创建

字面量

var num=10;
    console.log(num);
    console.log(typeof num);

new关键字

// var num2=new Number(value)
    // 当参数的值不能转换为数字时,将返回NaN
var num2=new Number(100);
    console.log(num2);
    console.log(typeof num2);

    var num3=new Number("100");
    console.log(num3);

    var num4=new Number("hello");
    console.log(num4);// NaN: Not a Number

02.Number对象的精度进制

js中 只用一种数字类型 包含整数和小数(浮点型)

var num1=100;
    var num2=100.0;

科学计数法 极大的或绩效的数字可以使用科学(指数)计数法表示 e:10的次方/次幂

var a=12300000000000;
    var b=123e11;
    console.log(a);
    console.log(b);
    console.log(a===b);
    var c=0.00000000000123;
    var d=1.23e-12;
    console.log(c);
    console.log(d);

精度整数(不适用于小数点或科学计数法) 最多15位

var x=999999999999999;
    var y=9999999999999999;
    console.log(x);
    console.log(y);

小数最大位数17位 运算不能保证100%准确

// var n=0.0000000000000001;
    // var m=0.00000000000000012;
    // console.log(n);
    // console.log(m)
    console.log(0.1+0.2);//0.30000000000000004

十进制转换为二进制 二进制转换为十进制 出现了偏差

进制
    // 十进制  逢十进一 默认 
    var i=210;//0*1+1*10+2*10*10
    // 十六进制  逢十六进一  前缀0x    0123456789abcdef
    var j=0xaa;//10*1+10*16 
    console.log(j);
    // 八进制  前缀 0
    var k=0211;//1*1+1*8+2*8*8
    console.log(k);//137
    // 二进制 
    
    // 进制的转换  toString(进制)
    var m=20;
    console.log(m.toString(8));//24   4*1+2*8
    console.log(m.toString(16));//14  4*1+1*16
    console.log(m.toString(2));//10100 0*1+0*2+1*2*2+0*2*2*2+1*2*2*2*2
    console.log(j.toString(10));// 170

03Number对象的属性

// constructor 返回对创建此对象的 Number 函数的引用。

// prototype 允许您可以向对象添加属性和方法。

console.log(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(-1/0);

    // POSITIVE_INFINITY	正无穷大,溢出时返回该值。
    console.log(Number.POSITIVE_INFINITY);//Infinity
    console.log(2/0);

    console.log(2/0===Number.POSITIVE_INFINITY);

    // NaN 非数字值
    console.log(Number.NaN);
    var a=NaN;
    var b=NaN;
    console.log(a==b);// false

ES6新增的

// 表示在 JavaScript 中最大的安全整数(253 - 1)。
    console.log(Number.MAX_SAFE_INTEGER);//9007199254740991
    // 表示在 JavaScript 中最小的安全整数(253 - 1)。
    console.log(Number.MIN_SAFE_INTEGER);//-9007199254740991
    
    // EPSILON: 表示 1 和比最接近 1 且大于 1 的最小 Number 之间的差别
    console.log(Number.EPSILON);//2.220446049250313e-16

04.Number对象的方法

// toString() 把数字转换为字符串,使用指定的基数。

// valueOf() 返回一个 Number 对象的基本数字值

console.log(Number.prototype);
    // toExponential()  把对象的值转换为指数(科学)计数法
    console.log((123.0000000001).toExponential());//1.230000000001e+2
    // toFixed() 把数字转换为字符串,结果的小数点后有指定位数的数字。
    console.log((123.12737461891).toFixed(2));
    // toPrecision() 把数字格式化为指定的长度。
    console.log((123.12737461891).toPrecision(5));


    // isFinite(参数) 检测指定参数是否不为无穷大。  参数为NaN 或是正负无穷返回false  其他返回 true
    console.log(Number.isFinite(123));
    // isNaN(参数) 判断参数是否不是一个数字
    console.log(Number.isNaN("aaaa"));

    // 用来判断给定的参数是否为整数。
    console.log(Number.isInteger(100.111))
    // 判断传入的参数值是否是一个"安全整数"。
    console.log(Number.isSafeInteger(9007199254740992))
举报

相关推荐

python(第十天)

【JavaSE 第十天】

web第十天

HCIP第十天

第十天学习

JS学习第十天

python第十天笔记

0 条评论