笔记索引
对象相关
值属性
1 2 3 4
| Infinity; NaN; undefined; null;
|
属性方法
1 2 3 4 5 6 7 8 9
| eval(string); isFinite(); isNaN(); parseFloat(); parseInt(); decodeURI(encodedURI); decodeURIComponent(); encodeURI(encodedURI); encodeURIComponent();
|
基本对象
1 2 3 4 5
| Object; Function; Boolean; Symbol; Error;
|
数字类
1 2 3 4 5
| Number; Number.isInteger(xxx); Number.isSafeInteger(xxx);
Math;
|
包装对象
注意:
- 不要使用 new Number()、new Boolean()、new String() 创建包装对象
- 用 parseInt() 或 parseFloat() 来转换任意类型到 number
- 用 String() 来转换任意类型到 string ,或者 toString() 方法;例如 (123).toString(),注意小括号
- 通常不必把任意类型转换为 boolean 再判断,因为可以直接写 if (myVar) {…}
- typeof 操作符可以判断出 number、boolean、string、function 和 undefined
- 判断 Array 要使用 Array.isArray(arr)
- 判断 null 请使用 myVar === null
- 判断某个全局变量是否存在用 typeof window.myVar === ‘undefined’
- 函数内部判断某个变量是否存在用 typeof myVar === ‘undefined’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| var n = new Number(123); typeof n;
const obj = { log: ["a", "b", "c"], get latest() { return this.log[this.log.length - 1]; }, };
obj.latest;
const obj = { log: ["a", "b", "c"], set latest(value) { this.log.push(value); }, };
obj.latest;
|
Date 日期对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| new Date(); new Date(value); new Date("2015-06-24T19:49:22.875+08:00"); new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds);
Date.prototype.getxxx(); Date.prototype.setxxx(); Date.prototype.toxxx();
Date.now(); Date.UTC(year, month, date, hrs, min, sec, ms); Date.parse;
|
Error 错误处理
1 2 3 4 5 6 7 8 9 10 11 12
| try { throw new Error(); } catch (error) { if (error instanceof Error) { } } finally { }
|
错误被抛出后会层层向上(冒泡),直到引擎报错,可以在合适位置“一网打尽”
JSON 对象
1 2
| var s = JSON.stringify(); JSON.parse();
|
Promise 承诺对象
创建
1 2 3 4 5 6 7 8 9 10
| let promise = new Promise(function (resolve, reject) { if (success) { resolve(value); } else { reject(error); } });
|
调用
1 2 3 4 5 6 7 8
| promise.then( (success_value) => { console.log(success_value); }, (reject_error) => { console.log(reject_error); }, );
|
实例化方法,返回一个对象
1 2 3 4
| new Promise.all(iterable); new Promise.race(iterable); new Promise.reject(reason); new Promise.resolve(value);
|
成员方法
1 2 3
| Promise.prototype.catch(onRejected); Promise.prototype.then(onFulfilled, onRejected); Promise.prototype.finally(onFinally);
|
使函数拥有 promise 功能
若欲使函数拥有 promise 功能,需让其返回一个 promise
1 2 3 4 5 6
| let promise = function (value) { value; new Promise((resolve, reject) => { Empty; }); };
|
通过连续使用 then 实现链式回调
从回调地狱
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| doSomething(function (result) { doSomethingElse( result, function (newResult) { doThirdThing( newResult, function (finalResult) { console.log("Got the final result: " + finalResult); }, failureCallback, ); }, failureCallback, ); }, failureCallback);
|
到链式调用
1 2 3 4 5 6 7 8 9 10 11 12 13
| doSomething() .then(function (result) { return doSomethingElse(result); }) .then(function (newResult) { return doThirdThing(newResult); }) .then(function (finalResult) { console.log("Got the final result: " + finalResult); }) .catch(failureCallback);
|
错误传递
抛出的错误会一直向后传递直到找到处理他的回调函数
RegExp 正则对象
创建对象
1 2
| var re1 = /ABC\-001/; var re2 = new RegExp("ABC\\-001");
|
常用方法
1 2 3
| re.test("010-12345");
"a,b, c d".split(/[\s\,]+/);
|