《你不知道的JavaScript(中卷)》读书笔记
中卷有点无聊,不过也是有干货的,但是好像要背一下的样子。不过作者大大都夸我是“优秀的开发人员”了,肯定要看完呀~~
第一部分。(图片太大了,只能转pdf再转的png,好像有点变形) 数组常用方法 ?:? 把一个数组和任意多的对象或数组连接,不改变当前数组。新生成的数组对于合成数组是浅拷贝。 let arr = [1,2,3= arr.concat('p',['x','y'
返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象。且原始数组不会被修改。 ['q','w','e','r'].slice(1,3)
['q','r'].slice()
['q','r'].slice(3)
array.splice(start[,deleteCount[,item1[,item2[,...]]]]):?从start开始,删除deleteCount个元素,并添加元素item1...itemn。返回被删除的元素集合,。 myFish = ["angel","clown","mandarin","surgeon"
removed = myFish.splice(2,"drum"
arr.join([separator]):将数组的每个元素通过分隔符连起来,亲测应该是通过for (i: 0...length)这种方式遍历的,所以空元素也会被加上。 parseInt的坑 parseInt( 0.000008 );
parseInt( 0.0000008 );
parseInt( ,16 );
parseInt( parseInt,16 );
parseInt( "0x10" );
parseInt( "103",2 );
JavaScript 中的相等 下卷就是Promise和生成器。 Promise之前研究了一下,就不写了,生成器是一个新东西。之前在用react的时候,只觉得field的用法很神奇,但完全没注意函数前面的*。后来才知道是生成器。 1. 函数名前面加 *?? *foo(x) {...}? 2. 通过运行函数得到生成器,传参和普通函数一样。? it = foo( 6 );? 3. 调用???函数开始运行 4. 函数运行到?yield?处会停止。 5. yield 的 传入的参数 是 yield 的返回值。注意,第一个next是不能传值的。 6.??? 可以向next?? res = next(..)?传递值。 <div class="cnblogs_code"> * y = x * (yield 'zzz'<span style="color: #0000ff;">var it = foo(6<span style="color: #000000;">) <span style="color: #0000ff;">var res =<span style="color: #000000;"> it.next() 迭代器。 iterable(可迭代),即指一个包含可以在其值上迭代的迭代器的对象。?
iterable 必须支持一个函数,其名称是专门的 ES6 符号值 Symbol.iterator 。调用这个函数时,它会返回一个迭代器。通常每次调用会返回一个全新的迭代器,虽然这一点并不是必须的。? 数字序列生成器实现标准的迭代器接口
something = (
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> {
[Symbol.iterator]: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() {
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">;
},next: </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() {
</span><span style="color: #0000ff;">if</span> (nextVal ===<span style="color: #000000;"> undefined) {
nextVal </span>= 1<span style="color: #000000;">
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
nextVal </span>= (3 * nextVal) + 6<span style="color: #000000;">
}
</span><span style="color: #0000ff;">return</span> { done: <span style="color: #0000ff;">false</span><span style="color: #000000;">,value: nextVal }
}
}
})() console.log(something.next().value) <span style="color: #008000;">//<span style="color: #008000;"> 1console.log(something.next().value) <span style="color: #008000;">//<span style="color: #008000;"> 9 console.log(something.next().value) <span style="color: #008000;">//<span style="color: #008000;"> 33 console.log(something.next().value) <span style="color: #008000;">//<span style="color: #008000;"> 105 可以通过 for...of 循环带有迭代器的对象。 (
(v > 500
当你执行一个生成器,就得到了一个迭代器。
生成器委托(感觉就是,这样也可以?)语法就是 yield * foo(),以下代码输出 12345 *"*foo() starting"34"*foo() finished"<span style="color: #0000ff;">function<span style="color: #000000;"> bar() {
yield 1<span style="color: #000000;">; yield 2<span style="color: #000000;">; yield<span style="color: #000000;"> foo(); yield 5<span style="color: #000000;">; } <span style="color: #0000ff;">var it =<span style="color: #000000;"> bar(); console.log(it.next().value) console.log(it.next().value) console.log(it.next().value) console.log(it.next().value) console.log(it.next().value) <span style="color: #008000;">/<span style="color: #008000;"> output <span style="color: #008000;">/ 1 2 <span style="color: #000000;">foo() starting 3 4 *<span style="color: #000000;">foo() finished 5 最近心情浮躁,看不下去了。待续。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |