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()
let someDate = new Date(Date.parse("May 23, 2019"));
let someDate = new Date("May 23, 2019"); // 等价
// January 1, 2000 at midnight GMT
let y2k = new Date(Date.UTC(2000, 0));
let y2k = new Date(2000, 0);
// May 5, 2005 at 5:55:55 PM GMT
let allFives = new Date(Date.UTC(2005, 4, 5, 17, 55, 55));
let allFives = new Date(2005, 4, 5, 17, 55, 55);
Date.now()
// get start time
let start = Date.now(); // 得到的是毫秒
// call a function
doSomething();
// get stop time
let stop = Date.now(),
result = stop – start;
Regular Expression 正则表达式
let expression = /pattern/flags;
flags
- g: global
- i: case-insensitive
- m: multi-line
- y: sticky
- u: unicode
下列metacharacters需要转义// Match all instances of "at" in a string.
let pattern1 = /at/g;
// Match the first instance of "bat" or "cat", regardless of case.
let pattern2 = /[bc]at/i;
// Match all three-character combinations ending with "at", regardless of case.
let pattern3 = /.at/gi;
( [ { \ ^ $ | ) ] } ? * + .// Match the first instance of "[bc]at", regardless of case.
let pattern2 = /\[bc\]at/i;
// Match all instances of ".at", regardless of case.
let pattern4 = /\.at/gi;
RegExp()构造函数
let pattern2 = new RegExp("[bc]at", "i");
PRIMITIVE WRAPPER TYPES
String(), Boolean(), Number()三种
当基本类型调用对象方法时,经历了三步
- 创建对象
- 调用方法
- 销毁对象
用new操作符创建的是对象,不用new则是类型转换
let a = new String("hello")
let b = new Boolean(true)
let c = new Number(1)
new Boolean(flase)转换为布尔值为true
Number
toFixed()指定小数点位数,四舍五入
let num = 10;
console.log(num.toFixed(2)); // "10.00"
let num = 10.005;
console.log(num.toFixed(2)); // "10.01"
toExponential()转换成e表达式
let num = 10;
console.log(num.toExponential(1)); // "1.0e+1"
toPrecision是toFixed和toExponential的综合
let num = 99;
console.log(num.toPrecision(1)); // "1e+2"
console.log(num.toPrecision(2)); // "99"
console.log(num.toPrecision(3)); // "99.0"
isInteger()
console.log(Number.isInteger(1)); // true
console.log(Number.isInteger(1.00)); // true
console.log(Number.isInteger(1.01)); // false