内建对象

150 阅读3分钟

Math

  1. Math.abs() 绝对值

    • 非负值
  2. Math.ceil()

    • 向上舍入,整数加 1
    console.log(Math.ceil(1.23)); //2
    
  3. Marh.floor()

    • 向下舍入,自动取整,不进行四舍五入
    console.log(Math.floor(1.23)); //1
    
  4. Math.round()

    • 四舍五入
    • 负数无法四舍五入
    console.log(Math.round(1.53)); //2
    console.log(Math.round(-1.53)); //-2
    
  5. Math.min()

    • 最小值
    • 处理数组的最小值
      Math.min.apply(null, arr);
      
  6. Math.max()

    • 最大值
    • 处理数组的最大值
      Math.max.apply(null, arr);
      
  7. Math.sqrt(4);

    • 平方根
    var n = Math.sqrt(4);
    console.log(n);
    
  8. Math.pow(底数,幂)

    var a = 10;
    var s = Math.pow(a, 2); //2次幂
    var s = Math.pow(a, 1 / 2); //平方根
    var s = Math.pow(a, 1 / 3); //立方根
    console.log(s);
    
  9. Math.random()

    • 生成 0-1 之间的随机数,不包括 1

Number

  • 属性
    1. Number.MAX_VALUE
    • 正数的最大值
    1. Number.MIN_VALUE
    • 正数的最小值
    1. Number.NaN
    • 非数值,等价于 NaN
  • 方法
    1. Number.toString()
      • 转字符串
    2. Number.toLocalString();
      • 本地数据格式
    3. Number.toPrecision(n)
      • n 在 1-21 之间
      • 保留 n 位数,将数值转换成字符串,以科学计数法显示,会进行四舍五入
    4. Number.toExponential(n)
      • 保留 n 位小数,不包含整数位,将数值转换成字符串,以科学计数法显示,会进行四舍五入
      • n=0 时,只有整数位

String

  • 属性
    1. length
      • 字符长度 只读,不可写
      • 字符串中包含多少个字符长度
      var str = "abcd";
      str.length = 0; //ES6  报错。ES5不报错,但无效
      console.log(str.length); //只能获取,不能修改
      
  • 方法
  1. str.charAt(n)

    • 获取下标为 n 的字符
  2. str.concat("eee")

    • 字符串拼接
    var age = 10;
    console.log("今年你".concat(age, "岁了"));
    
  3. str.indexOf("a",start,end)

    • 查找 a 字符是否存在,存在返回下标,不存在返回-1;
    • 从 start 开始,到 end 结束
  4. str.lastIndexOf("a")

    • 从尾部向前查找
  5. str.charCodeAt(n)

    • 获取下标是 n 的字符的 Unicode 编码
  6. String.fromCharCode()

    • 编码转换为字符串
  7. str.replace("a","z");

    • 字符串的方法不能修改原字符,返回替换后的字符串
    • 暂时只能替换一个字符
    var str = "abcdefg";
    var s = str.replace("a", "z"); //字符串的方法不能改变原字符串,返回替换后的新字符串
    console.log(str);
    console.log(s);
    
    str = str.replace("c", "c1"); //即是替换也是插入,与下面的代码等价
    
    str = str.replace("e", function (item) {
      return item + "1"; //即是替换也是插入
    });
    console.log(str);
    
  8. str.search("a")

    • 查找指定字符是否存在,存在返回下标,不存在返回-1
    • 可以用于正则表达式查找
    var str = "abcd";
    var index = str.search("e");
    console.log(index);
    
  9. str.match()

    • 字符串的正则表达式,将查找结果以数组的形式返回
  10. str.slice()

    • 与数组的 slice 等同
  11. str.substring(start,end)

    • end 和 start 都不支持负数,负数代表 0 之前,就是最前面

    • slice()只能从前向后选,substring()可以从后向前选

    • str.substring(start,length)

      • 截取字符串从 start 开始,按照给定的长度截取
  12. str.split()

    • 使用某个符号切割字符串,存储在数组中,与数组的 join()相反
  13. str.toUpperCase()

    • 小写转成大写
  14. str.toLowerCase()

    • 大写转小写

Date

- 每次new Date 就会获取new这个时间的当时值

```js
var date = new Date();
console.log(date);

var year = date.getFullYear(); //年
console.log(year);

var month = date.getMonth(); //月,0-11
console.log(month);

var day = date.getDate(); //日
var week = date.getDay(); //0是周日
var hour = date.getHours(); //小时
var minutes = date.getMinutes(); //分钟
var seconds = date.getSeconds(); //秒
var ms = date.getMilliseconds(); //毫秒

var h = date.getUTCDate(); //标准时间

var time = date.getTime(); //时间戳    从1970.1.1到现在的毫秒数,永远不重复,具有唯一性
// 作用:1.去除缓存;

var date = new Date();
console.log(date.toLocaleString()); //转换为本地时间
console.log(date.toUTCString()); //转换为格林尼治时间,后面的cookie使用

var date = new Date();
date.setMonth(12); //任何设置如果数值大于该值域的最大值时,就会进位

date.setMinutes(date.getMinutes() + 30); //设置30分钟后
console.log(date);
```