JS内置对象和内置函数:基本包装类型、Math对象、Date对象

内置对象和内置函数

1. 基本包装类型

  • 1)为了便于操作基本类型值,ECMAScript提供了3个特殊的引用类Boolean, Number, String
    每当读取一个基本类型值的时候,后台就会创建一个对应的基本包装类型对象,从而可以使我们调用一些方法操作这些数据。
    var s = “briup”;
    s.substring(2);
    后台会自动完成以下操作:
    a.创建String类型的一个实例
    b.在实例上调用指定的方法
    c.销毁这个实例

  • 2)基本包装类型的实例调用typeof返回"object",从而所有基本包装类型对象都会被转换为布尔类型的true.
    Object构造函数会像工厂方法一些,根据传入的值的类型返回相应基本包装类型的实例
    var obj = new Object(“briup”); //obj 类型为String包装类型
    console.log(obj instanceof String);
    使用new调用基本包装类型的构造函数,与直接调用同名的转换函数不一样
    var s = “11”;
    var s1 = Number(s); //转型函数 number类型
    var s2 = new Number(s); //构造函数 object类型

    1. Boolean,Number,不建议直接使用这两种类型
    1. String
    • length
      属性,获取字符串的字符数量
      charAt(i)
      返回给定位置的字符
      charCodeAt()
      返回给定位置的字符的字符编码
      var s = “helloworld”;
      s.charAt(1); //e
      s.charCodeAt(1); //101
      concat()
      将一个或多个字符串拼接起来,返回拼接得到的新字符串,但是大多使用"+“拼接
      slice()
      (开始位置,返回字符后一个字符位置)
      substr()
      (开始位置,返回字符个数)
      substring()
      (开始位置,返回字符后一个字符位置)
      var s = “helloworld”;
      s.slice(3,7); //lowo
      s.substr(3,7); //loworld
      s.substring(3,7);//lowo
      s //helloworld 不改变原值大小
      indexOf();
      从前往后查找指定字符所在位置
      lastIndexOf();
      从后往前查找字符串所在位置,可以有第二个参数,代表从字符串中哪个位置开始查找。
      trim();
      删除前置以及后置中的所有空格,返回结果
      var s = " hello world “;
      console.log(”|”+s.trim()+"|"); //|hello world|
      toLowerCase() :转换为小写
      toUpperCase() :转换为大写

2. Math对象

  • 1)常用方法

    • 1.比较方法
      Math.min() 求一组数中的最小值
      Math.max() 求一组数中的最大值
      Math.min(1,2,19,8,6); //1

    • 2.将小数值舍入为整数的几个方法:
      Math.ceil() 向上舍入
      Math.floor() 向下舍入
      Math.round() 四舍五入
      console.log(Math.ceil(12.41)); //13
      console.log(Math.floor(12.41)); //12
      console.log(Math.round(12.3)); //12
      console.log(Math.round(12.5)); //13

    • 3.随机数
      Math.random() 返回大于0小于1的一个随机数
      2)其他方法:(了解即可,即用即查)
      abs(num) 返回num绝对值
      exp(num) 返回Math.E的num次幂
      log(num) 返回num的自然对数
      pow(num,power) 返回num的power次幂
      sqrt(num) 返回num的平方根
      scos(x) 返回x的反余弦值
      asin(x) 返回x的反正弦值
      atan(x) 返回x的反正切值
      atan2(y,x) 返回y/x的反正切值
      cos(x) 返回x的余弦值
      sin(x) 返回x的正弦值
      tan(x) 返回x的正切值

3. Date对象

  • 将一个字符串转换为Date对象的写法:
    var str = “2019-12-12”;
    var date = new Date(str);   //字符串转换为Date对象
    document.write(date.getFullYear()); //然后就可以使用Date对象的方法输出年份了

  • Date.getDate()
    返回是日期对象中月份中的几号。
    var date = new Date();   //2019-12-19
    document.write(date.getDate()); //返回 19 是19号

  • Date.getDay()  
    返回日期中的星期几  星期天0-星期6
    var date = new Date();
    document.write(date.getDay()); //3 星期3

  • Date.getFullYear()  
    返回年份 如2019。
    var date = new Date();
    document.write(date.getFullYear());  //返回2019,2019年

  • Date.getHours()  
    返回日期中的小时,几点了,0-23
    var date = new Date();
    document.write(date.getHours());  //返回23,晚上11点

  • Date.getTime()      
    将一个日期对象以毫秒形式返回
    var date = new Date();
    document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。

  • Date.getTimezoneOffset()   
    GMT时间与本地时间差,用分钟表示
    var date = new Date();
    document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。

  • Date.getUTCMilliserconds()  
    返回Date对象中的毫秒数,(全球时间)
    var date = new Date();
    document.write(date.getMilliseconds());  //返回全球时间中的毫秒数

  • Date.getUTCMinutes()    
    返回Date对象中的分钟数,(全球时间)
    var date = new Date();
    document.write(date.getMinutes());  //2019-12-19 23:49  返回49,注意是全球时间

  • Date.getUTCSeconds()    
    返回Date对象中的秒数值
    var date = new Date();
    document.write(date.getSeconds());  //返回秒数值 返回33

  • Date.now()    
    静态方法  //返回1970-01-01午夜到现在的时间间隔,用毫秒表述
        document.write(Date.now());  //静态方法,返回当前时间与1970-01-01的时间间隔,毫秒单位。

  • var da = new Date(date);
    document.write("
    " + da.getFullYear() + “-” + da.getMonth() + “-” + da.getDate());  //输出2019-11-19  //注意月份是从0-11

  • Date.parse()    
    解析一个日期时间字符串,返回1970-01-01午夜到给定日期之间的毫秒数
    var date = “2019-12-19”;
    document.write(Date.parse(date));  //返回  1355875200000

  • Date.getYear()    
    返回Date对象中的年份值减去1900
    var date = new Date();
    document.write(date.getYear());  //2019-12-19  返回112 (2019-1900)

  • Date.getUTCMonth()    
    返回Date对象中月份值,(全球时间)
    var date = new Date();
    document.write(date.getMonth());  //2019-12-19  返回11,0(1月份)-11(12月份)

  • Date.getUTCHours()  
    返回Date对象中的小时数,就是现在是几点,终于有一个跟getHours()不同了,应该是时差关系,返回的是全球时间里的。
    var date = new Date();
    document.write(date.getUTCHours());  //现在北京时间是2019-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。

  • Date.getUTCFullYear()  
    返回Date中的年份,4位,如2019,(全球时间)
    var date = new Date();
    document.write(date.getUTCFullYear());  //返回2019

  • Date.getUTCDate()    
    返回Date对象中的日期值,(全球时间)
    var date = new Date();
    document.write(date.getUTCDate());  //返回19  19号

  • Date.getUTCDay()    
    返回Date对象中的星期几,(全球时间)
    var date = new Date();
    document.write(date.getUTCDay());  //返回3  星期3

  • Date.getMilliseconds()  
    返回日期中的毫秒数
    var date = new Date();
    document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒

  • Date.getMinutes()    
    返回日期中的分钟数  0-59
    var date = new Date();
    document.write(date.getMinutes());  //2019-12-19 23:22  返回22,12点22分

  • Date.getMonth()   
      返回日期中的月份数,返回值0(1月)-11(12月)
    var date = new Date();
    document.write(date.getMonth());  //2019-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11

  • Date.getSeconds()    
    返回一个日期的描述
    var date = new Date();
    document.write(date.getSeconds());·//返回34,2019-12-19 23:27:34  27分34秒

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值