JavaScript学习第十天

JavaScript的布尔对象

     Boolean对象用于将非布尔值转换为布尔值(true或false)
    1.  var a=true;
        var b=false;
    // var c=new Boolean(参数) 参数可以非布尔值 自动转换
    var c=new Boolean("");
    console.log(c)

    2. 属性
    console.log(Boolean.prototype)
    // constructor 返回对创建此对象的 Boolean 函数的引用
    // prototype 给对象添加属性和方法   原型

    3. 方法
    // 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);    

JavaScript的Date对象的方法

     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)。
    ⑤ setMinutes()	设置 Date 对象中的分钟 (0 ~ 59)。
    ⑥ setSeconds()	设置 Date 对象中的秒钟 (0 ~ 59)。  ⑦ setMilliseconds()	设置 Date 对象中的毫秒 (0 ~ 999)。

    // 设置世界时间  格林威治时间
     ①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)     

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对象的方法

    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    

Number对象的创建

    1.字面量
    var num=10;
    console.log(num);
    console.log(typeof num);
    2.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     

Number对象的精度进制

    // js中 只用一种数字类型 包含整数和小数(浮点型)
    var num1=100;
    var num2=100.0;

    1. 科学计数法 极大的或绩效的数字可以使用科学(指数)计数法表示  
    // 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);

    2. 精度
    // 整数(不适用于小数点或科学计数法) 最多15位
    var x=999999999999999;
    var y=9999999999999999;
    console.log(x);
    console.log(y);

    3. 小数
     //最大位数17位   运算不能保证100%准确
     var n=0.0000000000000001;
     var m=0.00000000000000012;
     console.log(n);
     console.log(m)
    console.log(0.1+0.2);//0.30000000000000004
     //十进制转换为二进制  二进制转换为十进制  出现了偏差

    4. 进制
    ① 十进制  逢十进一 默认 
    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
   
    5. 进制的转换  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    

Number对象的属性

    ① 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新增的
    
    a. 表示在 JavaScript 中最大的安全整数(253 - 1)。
    console.log(Number.MAX_SAFE_INTEGER);//9007199254740991
    b. 表示在 JavaScript 中最小的安全整数(253 - 1)。
    console.log(Number.MIN_SAFE_INTEGER);//-9007199254740991
    
    c. EPSILON: 表示 1 和比最接近 1 且大于 1 的最小 Number 之间的差别
    console.log(Number.EPSILON);//2.220446049250313e-1

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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值