当前位置: 首页 > news >正文

南宁专业网站开发制作网站免费建站

南宁专业网站开发,制作网站免费建站,做网站配置好了找不到服务器,软件定制开发服务收费多少ES6#xff1a;Generator函数详解 1、 概念2、yield表达式2.1 yield 语句与 return 语句区别2.2 Generator函数不加yield语句#xff0c;这时变成了一个单纯的暂缓执行函数2.3 yield 表达式只能用在 Generator 函数里面#xff0c;用在其它地方都会报错2.4 yield 表达式如果… ES6Generator函数详解 1、 概念2、yield表达式2.1 yield 语句与 return 语句区别2.2 Generator函数不加yield语句这时变成了一个单纯的暂缓执行函数2.3 yield 表达式只能用在 Generator 函数里面用在其它地方都会报错2.4 yield 表达式如果用在另一个表达式中必须放在圆括号里面2.5 yield 表达式用作参数或放在赋值表达式的右边可以不加括号 3、与 Iterator 接口的关系4、next() 方法的参数5、for...of循环6、Generator.prototype.throw()7、Generator.prototype.return()8、yield*表达式9、Generator函数应用举例9.1 利用 Generator 函数和 for...of 循环实现斐波那契数列9.2 异步操作的同步化表达9.3 逐行读取文本文件9.4 控制流管理9.5 Genarator 对任意对象部署Iterator接口9.6 Genarator 对数组部署Iterator接口 10、promise、generator和aysnc/await区别10.1 promise10.2 Generator10.3 async/await 1、 概念 Generator函数式ES6提供的一种异步编程解决方案语法行为与传统函数完全不同。对于Generator函数有多种理解角度。 从语法上可以把它理解成一个状态机封装了多个内部状态 从形式上Generator 函数是一个普通函数但是有两个特征一是function命令与函数名之间有一个星号*二是函数体内部使用yield语句定义不同的内部状态。 // 传统函数 function fn() {return hello world } fn() // hello world一旦调用立即执行// Generator函数 function* helloWorldGenerator () {yield helloyield worldreturn ending }const hw helloWorldGenerator() // 调用 Generator函数函数并没有执行返回的是一个Iterator对象 console.log(hw.next()) // {value: hello, done: false}value 表示返回值done 表示遍历还没有结束 console.log(hw.next()) // {value: world, done: false}value 表示返回值done 表示遍历还没有结束 console.log(hw.next()) // {value: ending, done: true}value 表示返回值done 表示遍历还没有结束 console.log(hw.next()) // {value: undefined, done: true}value 表示返回值done 表示遍历结束上面的代码中可以看到传统函数和Generator函数的运行是完全不同的传统函数调用后立即执行并输出了返回值Generator函数则没有执行而是返回一个Iterator对象并通过调用Iterator对象的next方法来遍历每次调用Iterator对象的next方法时内部的指针就会从函数的头部或上一次停下来的地方开始执行直到遇到下一个 yield 表达式或return语句暂停。换句话说Generator 函数是分段执行的yield表达式是暂停执行的标记而 next方法可以恢复执行。 执行过程如下 第1次调用Generator 函数开始执行直到遇到第一条 yield 语句为止。next 方法返回一个对象它的 value 属性就是当前 yield 语句的值 hellodone 属性的值 false 表示遍历还没有结束。第2次调用Generator 函数从上次 yield 语句停下的地方一直执行到下一条 yield 语句。next 方法返回的对象的 value 属性就是当前 yield 语句的值 worlddone 属性的值false 表示遍历还没有结束。第 3 次调用Generator 函数从上次 yield 语句停下的地方一直执行到 return 语句(如果没有 return 语句就执行到函数结束)。next 方法返回的对象的 value 属性就是紧跟在return 语句后面的表达式的值(如果没有 return 语句,则 value 属性的值为 undefined),done 属性的值 true 表示遍历已经结束。第 4 次调用此时 Generator 函数已经运行完毕next 方法返回的对象的 value 属性为undefineddone 属性为 true。以后再调用 next 方法返回的都是这个值。 2、yield表达式 由于 Generator 函数返回的遍历器对象只有调用 next 方法才会遍历下一个内部状态所以其实提供了一种可以暂停执行的函数。yield 语句就是暂停标志。 遍历器对象的 next 方法的运行逻辑如下。 1.遇到 yield 语句就暂停执行后面的操作并将紧跟在 yield 后的表达式的值作为返回的对象的 value 属性值。 2.下一次调用 next 方法时再继续往下执行直到遇到下一条 yield 语句。 3.如果没有再遇到新的 yield 语句就一直运行到函数结束直到 return 语句为止,并将 return 语句后面的表达式的值作为返回对象的value 属性值。 4.如果该函数没有 return 语句则返回对象的 value 属性值为undefined。 注意只有调用next方法且内部指针指向该语句时才会执行yield语句后面的表达式因此等于为JavaScript提供了手动的“惰性求值”的语法功能。 function* gen() {yield 123 456; }上面的代码中yield 后面的表达式 123 456 不会立即求值只会在 next 方法将指针移到这一句时才求值。 2.1 yield 语句与 return 语句区别 yield 语句与 return 语句既有相似之处又有区别。相似之处在于都能返回紧跟在语句后的表达式的值。区别在于每次遇到 vield 函数暂停执行下一次会从该位置继续向后执行而 return 语句不具备位置记忆的功能。一个函数里面只能执行一次(或者说一条) return语句但是可以执行多次(或者说多条) yield 语句。正常函数只能返回一个值因为只能执行一次 return 语句;Generator 函数可以返回一系列的值因为可以有任意多条 yield 语句。从另一个角度看也可以说 Generator 生成了一系列的值这也就是其名称的来历(在英语中generator”这个词是“生成器”的意思)。 2.2 Generator函数不加yield语句这时变成了一个单纯的暂缓执行函数 function* f () {console.log(执行了) } var generator f() setTimeout(function () {generator.next() }, 2000)上面的代码中函数 f 如果是普通函数在为变量 generator 赋值时就会执行。但是函数 f是一个 Generator 函数于是就变成只有调用 next 方法时才会执行。 2.3 yield 表达式只能用在 Generator 函数里面用在其它地方都会报错 {(function (){yield 1;})()// SyntaxError: Unexpected number// 在一个普通函数中使用yield表达式结果产生一个句法错误 } 2.4 yield 表达式如果用在另一个表达式中必须放在圆括号里面 {function* demo() {console.log(Hello yield); // SyntaxErrorconsole.log(Hello yield 123); // SyntaxErrorconsole.log(Hello (yield)); // OKconsole.log(Hello (yield 123)); // OK} } 2.5 yield 表达式用作参数或放在赋值表达式的右边可以不加括号 {function* demo() {foo(yield a, yield b); // OKlet input yield; // OK} } 3、与 Iterator 接口的关系 任意一个对象的 Symbol.iterator 方法等于该对象的遍历器对象生成函数调用该函数会返回该对象的一个遍历器对象。 由于 Generator 函数就是遍历器生成函数因此可以把 Generator 赋值给对象的Symbol.iterator 属性从而使得该对象具有 Iterator 接口。 var myIterable {} myIterable[Symbol.iterator] function* () {yield 1yield 2yield 3 }for(let value of myIterable) {console.log(value)}// 1// 2// 3 [...myIterable] // [1, 2, 3]上面的代码中Generator 函数赋值给 Symbol.iterator 属性从而使得 myIterable对象具有了 Iterator 接口可以被...运算符遍历。 4、next() 方法的参数 yield 语句本身没有返回值,或者说总是返回 undefined。next 方法可以带有一个参数该参数会被当作上一条 yield 语句的返回值。 function* f () {for (var i 0; true; i) {var reset yield iif (reset) {console.log(执行了)i -1}} } var g f() g.next() // {value: 0, done: false} g.next() // {value: 1, done: false} g.next(true) // {value: 0, done: false}上面的代码先定义了一个可以无限运行的 Generator 函数 f如果 next 方法没有参数每次运行到 yield 语句时,变量 reset 的值总是 undefined。当next 方法带有一个参数 true时当前的变量 reset 就被重置为这个参数(即 true)因而 i 会等于-1下一轮循环就从-1开始递增。 这个功能有很重要的语法意义。Generator 函数从暂停状态到恢复运行其上下文状态(context)是不变的。通过 next 方法的参数就有办法在Generator 函数开始运行后继续向函数本内部注入值。也就是说可以在 Generator 函数运行的不同阶段从外部向内部注入不同的值从而调整函数行为。 再看一个例子。 function* foo (x) {var y 2 * (yield (x 1))var z yield (y / 3)return (x y z) }var a foo(5) a.next() // 首次调用next函数只会执行到 “yield(51)” 暂停并返回 {value: 6, done: false} a.next() // 第二次调用next没有传递参数所以 y的值是undefined那么 y/3 当然是一个NaN所以应该返回 {value: NaN, done: false} a.next() // 同样的道理z也是undefined6 undefined undefined NaN返回 {value: NaN, done: true}var b foo(5) b.next() // 正常的运算应该是先执行圆括号内的计算再去乘以2由于圆括号内被 yield 返回 5 1 的结果并暂停所以返回{value: 6, done: false} b.next(12) // 上次是在圆括号内部暂停的所以第二次调用 next方法应该从圆括号里面开始就变成了 let y 2 * (12)y被赋值为24所以第二次返回的应该是 24/3的结果 {value: 8, done: false} b.next(13) // 参数2被赋值给了 z最终 x y z 5 24 13 42返回 {value: 42, done: true}5、for…of循环 for...of 循环可以自动遍历 Generator 函数生成的 Iterator 对象且此时不再需要调用next 方法。 function* foo () {yield 1yield 2yield 3yield 4yield 5return 6 } for (let v of foo()) {console.log(v) } // 1 2 3 4 5上面的代码使用 for...of 循环依次显示 5条 yield 语句的值。 注意一旦 next 方法的返回对象的 done 属性为 truefor…of 循环就会终止且不包含该返回对象所以上面的 return 语句返回的 6 不包括在 for…of 循环中。 6、Generator.prototype.throw() Generator 函数返回的遍历器对象都有一个 throw 方法可以在函数体外抛出错误然后在 Generator 函数体内捕获。 var g function* () {try (yield;} catch (e) {console.log(内部捕获’e);} } var ig(); i.next();try {i.throw(a);i.throw(b); } catch (e) {console.log(外部捕获’e); } //内部捕获a // 外部捕获b上面的代码中遍历器对象 i 连续抛出两个错误。第一个错误被 Generator 函数体内的catch 语句捕获。i 第二次抛出错误由于 Generator 函数内部的 catch 语句已经执行过了不会再捕捉到这个错误了所以这个错误就被抛出了 Generator 函数体被函数体外的 catch语句捕获。 7、Generator.prototype.return() Generator 函数返回的遍历器对象还有一个 return 方法可以返回给定的值并终结Generator 函数的遍历。 function* gen() {yield 1;yield 2;yield 3; } var g gen(); g.next () // { value: 1, done: false } g.return(foo) // { value:foodone: true } g.next() // { value: undefined, done: true }上面的代码中遍历器对象 g 调用 return 方法后返回值的 value 属性就是 return方法的参数 foo。同时Generator 函数的遍历终止返回值的 done 属性为 true以后再调用 next 方法done 属性总是返回 true。 如果 return 方法调用时不提供参数则返回值的 vaule 属性为 undefined。 function* gen() {yield 1;yield 2;yield 3; } var g gen(); g.next() // { value: 1,done: false } g.return() // { value: undefined,done: true }8、yield*表达式 如果在 Generator函数内部调用另一个 Generator 函数默认情况下是没有效果的。 function* foo() {yield a;yieldb; } function* bar() {yieldx;foo();yield y; } for (let v of bar()){console.log(v); } //x //y上面的代码中foo 和 bar 都是 Generator 函数在 bar 里面调用 foo 是不会有效果的。 这时就需要用到 yield*语句用来在一个 Generator 函数里面执行另一个 Generator 函数。 function* bar() {yield x;yield* foo();yieldy; } // 等同于 function* bar() [yield x;yield a;yield b;yield y; } // 等同于 function* bar() {yield x;for (let v of foo()) {yield v;}yield y; } for (let v of bar()) {console.log(v); } // x; // a; // b; // y;9、Generator函数应用举例 9.1 利用 Generator 函数和 for…of 循环实现斐波那契数列 function* fibonacci() {let [prevcurr] [01];for (;;) {[prev, curr] [curr, prev currl;yield curr;} } for (let n of fibonacci()) {if (n 1000) break;console.log(n); } // 1 2 3 5 8 13 21 34 55 89 144 233 377 610 9879.2 异步操作的同步化表达 Generator 函数的暂停执行的效果意味着可以把异步操作写在yield表达式里面等到调用next方法时再往后执行。这实际上等同于不需要写回调函数了因为异步操作的后续操作可以放在yield表达式下面反正要等到调用next方法时再执行。所以Generator 函数的一个重要实际意义就是用来处理异步操作改写回调函数。 function* main() {var result yield request(http://some.url);var resp JSON.parse(result);console.log(resp.value); } function request(url) {makeAjaxCall(url, function(response){it.next(response);}); } var it main(); it.next();上面代码的main函数就是通过 Ajax 操作获取数据。可以看到除了多了一个yield它几乎与同步操作的写法完全一样。 注意makeAjaxCall函数中的next方法必须加上response参数因为yield表达式本身是没有值的总是等于undefined。 9.3 逐行读取文本文件 function* numbers() {let file new FileReader(numbers.txt);try {while(!file.eof) {yield parseInt(file.readLine() 10);}} finally {file.close();} }上面的代码打开文本文件使用 yield 语句可以手动逐行读取文件。 9.4 控制流管理 如一个多步操作非常耗时采用回调的话 step1(function (value1) {step2(value1, function(value2) {step3(value2, function(value3) {step4(value3, function(value4) {// Do something with value4});});}); });采用Promise改写上面的代码如下 Promise.resolve(step1).then(step2).then(step3).then(step4).then(function (value4) {// Do something with value4}, function (error) {// Handle any error from step1 through step4}).done();上面的代码已经把回调函数改成了直线执行的形式但是加入了大量 Promise 的语法。Generator 函数可以进一步改善代码运行流程。 function* longRunningTask(valuel) {try {var value2 yield stepl(valuel);var value3 yield step2(value2);var value4 yield step3(value3);var value5 yield step4(value4);// Do something with value4} catch (e) {// Handle any error from stepl through step4} }9.5 Genarator 对任意对象部署Iterator接口 function* deployObjectInterface(obj){let keys Object.keys(obj);for(let i0; ikeys.length; i){let key keys[i];yield [key, obj[key]];} } let obj {name:jow, age:21 }; for(let[key, value] of deployObjectInterface(obj)){console.log(key, value); } // name jow // age 219.6 Genarator 对数组部署Iterator接口 function* deployArrayInterface(arr){var nextIndex 0;while(nextIndex arr.length){yield arr[nextIndex];} } var arr deployArrayInterface([name, age]);console.log(arr.next()); // {value: name, done: false} console.log(arr.next().value); // name console.log(arr.next().done); // falseconsole.log(arr.next().value); // age console.log(arr.next().done); // trueconsole.log(arr.next().value); // undefined console.log(arr.next().done); // true10、promise、generator和aysnc/await区别 三者都是异步编程的解决方案,不同的是promise为较早出来的,其次generator最后为async/await三者象征了前端进行解决异步编程的进化路程。 10.1 promise promise比较简单也是最常用的主要就是将原来用回调函数异步编程的方法转成relsove和reject触发事件对象内含有四个方法then()异步请求成功后catch()异步请求错误的回调方法finally()请求之后无论是什么状态都会执行resolve()将现有对象转换为Promise对象all()此方法用于将多个Promise实例包装成一个新的promise实例。race()也是将多个Promise实例包装成一个新的promise实例reject()返回一个状态为Rejected的新Promise实例。优点让回调函数变成了规范的链式写法程序流程可以看的很清楚缺点:编写的难度比传统写法高阅读代码也不是一眼可以看懂10.2 Generator generator是一个迭代生成器,其返回值为迭代器(lterator),是ES6标准引入的新的数据类型主要用于异步编程,它借鉴于Python中的generator概念和语法generator函数内有两个重要方法1 yield表达式 2.next()Generator 函数是分段执行的yield表达式是暂停执行的标记而 next方法可以恢复执行优点1.利用循环每调用一次就使用一次不占内存空间 2.打破了普通函数执行的完整性 缺点: 需要用next()方法手动调用,直接调用返回无效iterator 2.10.3 async/await async异步函数 await同步操作es7中提出来的异步解决方法,是目前解决异步编程终极解决方案,以promise为基础,其实也就是generator的高级语法糖,本身自己就相当于一个迭代生成器(状态机),它并不需要手动通过next()来调用自己,与普通函数一样async就相当于generator函数中的*,await相当于yield,async 用于申明一个 function 是异步的而 await 用于等待一个异步方法执行完成。 function getSomething() {return something; }async function testAsync() {return Promise.resolve(hello async); }async function test() {//await是在等待一个async函数完成const v1 await getSomething();//await后面不仅可以接Promise还可以接普通函数或者直接量const v2 await testAsync();console.log(v1, v2); }
文章转载自:
http://www.morning.srgwr.cn.gov.cn.srgwr.cn
http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn
http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn
http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn
http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn
http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn
http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn
http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn
http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn
http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn
http://www.morning.ptslx.cn.gov.cn.ptslx.cn
http://www.morning.sbwr.cn.gov.cn.sbwr.cn
http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn
http://www.morning.rscrj.cn.gov.cn.rscrj.cn
http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn
http://www.morning.slkqd.cn.gov.cn.slkqd.cn
http://www.morning.rqhn.cn.gov.cn.rqhn.cn
http://www.morning.ailvturv.com.gov.cn.ailvturv.com
http://www.morning.djpzg.cn.gov.cn.djpzg.cn
http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn
http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn
http://www.morning.jqtb.cn.gov.cn.jqtb.cn
http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn
http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn
http://www.morning.grcfn.cn.gov.cn.grcfn.cn
http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn
http://www.morning.qngcq.cn.gov.cn.qngcq.cn
http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn
http://www.morning.wjrq.cn.gov.cn.wjrq.cn
http://www.morning.xnqwk.cn.gov.cn.xnqwk.cn
http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn
http://www.morning.cmfkp.cn.gov.cn.cmfkp.cn
http://www.morning.nqgds.cn.gov.cn.nqgds.cn
http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn
http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn
http://www.morning.byrlg.cn.gov.cn.byrlg.cn
http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn
http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn
http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn
http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn
http://www.morning.itvsee.com.gov.cn.itvsee.com
http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn
http://www.morning.lmmh.cn.gov.cn.lmmh.cn
http://www.morning.wjxtq.cn.gov.cn.wjxtq.cn
http://www.morning.kgqpx.cn.gov.cn.kgqpx.cn
http://www.morning.pngph.cn.gov.cn.pngph.cn
http://www.morning.ksgjn.cn.gov.cn.ksgjn.cn
http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn
http://www.morning.gcqdp.cn.gov.cn.gcqdp.cn
http://www.morning.bwqr.cn.gov.cn.bwqr.cn
http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn
http://www.morning.rwzc.cn.gov.cn.rwzc.cn
http://www.morning.jjhng.cn.gov.cn.jjhng.cn
http://www.morning.nykzl.cn.gov.cn.nykzl.cn
http://www.morning.tfei69.cn.gov.cn.tfei69.cn
http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn
http://www.morning.psxcr.cn.gov.cn.psxcr.cn
http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn
http://www.morning.ntyks.cn.gov.cn.ntyks.cn
http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn
http://www.morning.znrlg.cn.gov.cn.znrlg.cn
http://www.morning.rqbr.cn.gov.cn.rqbr.cn
http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn
http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn
http://www.morning.rfzzw.com.gov.cn.rfzzw.com
http://www.morning.cwtrl.cn.gov.cn.cwtrl.cn
http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn
http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn
http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn
http://www.morning.mnsts.cn.gov.cn.mnsts.cn
http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn
http://www.morning.prznc.cn.gov.cn.prznc.cn
http://www.morning.fksyq.cn.gov.cn.fksyq.cn
http://www.morning.lkthj.cn.gov.cn.lkthj.cn
http://www.morning.zhffz.cn.gov.cn.zhffz.cn
http://www.morning.bpmtl.cn.gov.cn.bpmtl.cn
http://www.morning.mksny.cn.gov.cn.mksny.cn
http://www.morning.msbct.cn.gov.cn.msbct.cn
http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn
http://www.morning.kjfqf.cn.gov.cn.kjfqf.cn
http://www.tj-hxxt.cn/news/248175.html

相关文章:

  • 外贸视频网站开发为什么做电影网站没有流量
  • 网站开发公司 上wordpress网盘搜索
  • 网上做兼职的网站frontpage做内部网站
  • 网页上传和网站开发游戏软件开发公司简介
  • 新乡网站建设制作公司淮北招聘网淮北论坛
  • developer官网下载东莞优化哪家好
  • wordpress说说插件企业网站产品内页优化
  • 做网站的桔子什么品牌推广策略案例
  • 上海兴业建设有限公司网站推荐家居网站建设
  • 音频网站建设做企业网站电话销售话术
  • 郑州好的网站建设公司教育网站制作哪家服务好
  • 南山老品牌网站建设防伪码网站怎么做
  • 公司网站的管理和维护橙色wordpress模板
  • 有哪些好的模板网站睢宁建网站
  • 如何把旅行社网站做的好看一家只做家纺的网站
  • 技术网站源码wordpress新手建什么网站赚钱
  • 凡科论坛网站制作北京做网站维护
  • 如何做一份企业网站规划广州最新新闻头条
  • 扬州公司做网站公司哪家好吗网站建设
  • 公司要做网站做免费漫画网站有风险吗
  • 网站营销网站优化开发一个app需要多少钱 知乎
  • 什么网站可以做公共基础知识企业门户网模板下载
  • js 网站开发罗湖商城网站设计电话
  • 网站开发的prd 怎么写网站建设类岗位有哪些
  • 微信小程序里的网站怎么做wordpress访客主机名
  • 漳州微信网站建设wordpress淘宝插件
  • 哇哈哈网站建设策划书注册企业邮箱哪家最好
  • 长清区网站建设宣传湖北省建设厅乡镇污水官方网站
  • 部署iis网站佛山市网站建设 骏域动力
  • 企业综合门户型网站iOS开发 隐私政策网站怎么做