💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
#### async 自动执行的generator + promise ``` function *main() { var res = yield step1(); res = yield step2(res); res = yield Promise.all([step3a(res), step3b(res)]); yield step4(res); } run(main).then(() => console.log('succeed'), () => console.log(err)); //async async function main() { var res = await step1(); res = await step2(res); res = await Promise.all([step3a(res), step3b(res)]); await step4(res); } main().then(() => console.log('succeed'), () => console.log(err)) //async函数有一个没有解决的问题,这也是它的缺点,就是无法从外部取消正在运行的async,你只能等到它运行结束然后操作它返给你的promise ``` #### Object.observe(已废弃) ``` var obj = {a: 1, b: 2}; Object.observe( obj, function(changes) { for(var change of changes) { console.log(change); } }, ["add", "update", "delete"] ); obj.c = 3; //{name: "c", object: obj, type: "add"} obj.a = 42; //{name: "a", object: obj, type: "update", oldValue: 1} delete obj.b; //{name: "b", object: obj, type: "delete", oldValue: 2} ``` ``` var obj = {a: 1, b: 2}; Object.observe(obj, function observer(changes) { for(var change of changes) { if(change.type == "setPrototype") { Object.unobserve(change.object, observer); break; } } }) ``` #### 幂运算 ``` a ** 4; //Math.pow(a, 4) a **= 3; //a = Math.pow(a, 3) ``` #### 对象属性与... ``` o3 = {...o1, ...o2, {a: 1}}; //clear var o1 = {b: 2, c: 3, d: 4} var {b, ...o2} = o1; b; //2 o2; //{c: 3, d: 4} ``` #### Array.includes ``` var vals = ["foo", "bar", 42, "baz"]; //查找42的话需要用indexOf,但是indexOf会返回-1或者大于等于0的值,不利于转换成bool //因此用 !!(~vals.indexOf(42)); //true (~a=-(a+1)) //es7 vals.includes(42); //true ```