objects are considered to be instances of a particular reference type.
New objects are created by using the new operator followed by a constructor. A constructor is simply a function whose purpose is to create a new object.

Date()

  1. let someDate = new Date(Date.parse("May 23, 2019"));
  2. let someDate = new Date("May 23, 2019"); // 等价
  3. // January 1, 2000 at midnight GMT
  4. let y2k = new Date(Date.UTC(2000, 0));
  5. let y2k = new Date(2000, 0);
  6. // May 5, 2005 at 5:55:55 PM GMT
  7. let allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55));
  8. let allFives = new Date(2005, 4, 5, 17, 55, 55);

Date.now()

  1. // get start time
  2. let start = Date.now(); // 得到的是毫秒
  3. // call a function
  4. doSomething();
  5. // get stop time
  6. let stop = Date.now(),
  7. result = stop start;

Regular Expression 正则表达式

let expression = /pattern/flags;

flags

  • g: global
  • i: case-insensitive
  • m: multi-line
  • y: sticky
  • u: unicode
    1. // Match all instances of "at" in a string.
    2. let pattern1 = /at/g;
    3. // Match the first instance of "bat" or "cat", regardless of case.
    4. let pattern2 = /[bc]at/i;
    5. // Match all three-character combinations ending with "at", regardless of case.
    6. let pattern3 = /.at/gi;
    下列metacharacters需要转义
    ( [ { \ ^ $ | ) ] } ? * + .
    1. // Match the first instance of "[bc]at", regardless of case.
    2. let pattern2 = /\[bc\]at/i;
    3. // Match all instances of ".at", regardless of case.
    4. let pattern4 = /\.at/gi;

    RegExp()构造函数

    1. let pattern2 = new RegExp("[bc]at", "i");

    PRIMITIVE WRAPPER TYPES

    String(), Boolean(), Number()三种
    当基本类型调用对象方法时,经历了三步
  1. 创建对象
  2. 调用方法
  3. 销毁对象

用new操作符创建的是对象,不用new则是类型转换

  1. let a = new String("hello")
  2. let b = new Boolean(true)
  3. let c = new Number(1)

new Boolean(flase)转换为布尔值为true

Number

toFixed()指定小数点位数,四舍五入

  1. let num = 10;
  2. console.log(num.toFixed(2)); // "10.00"
  3. let num = 10.005;
  4. console.log(num.toFixed(2)); // "10.01"

toExponential()转换成e表达式

  1. let num = 10;
  2. console.log(num.toExponential(1)); // "1.0e+1"

toPrecision是toFixed和toExponential的综合

  1. let num = 99;
  2. console.log(num.toPrecision(1)); // "1e+2"
  3. console.log(num.toPrecision(2)); // "99"
  4. console.log(num.toPrecision(3)); // "99.0"

isInteger()

  1. console.log(Number.isInteger(1)); // true
  2. console.log(Number.isInteger(1.00)); // true
  3. console.log(Number.isInteger(1.01)); // false