复习前面的

0h0m

用…运算符,重写之前的

2min21s

  1. let obj = {
  2. start: [12, 3, 4, 5],
  3. end: [7, 8, 9],
  4. [Symbol.iterator]() {
  5. let index = 0,
  6. arr = [...this.start, ...this.end],
  7. len = arr.length;
  8. return {
  9. next() {
  10. if (index < len) {
  11. return {
  12. value: arr[index++],
  13. done: false
  14. }
  15. } else {
  16. return {
  17. value: undefined,
  18. done: true
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. var iter = obj[Symbol.iterator]();
  26. console.log(iter.next());//{ value: 12, done: false }
  27. console.log(iter.next());//{ value: 3, done: false }
  28. console.log(iter.next());//{ value: 4, done: false }
  29. console.log(iter.next());//{ value: 5, done: false }
  30. console.log(iter.next());//{ value: 7, done: false }
  31. console.log(iter.next());//{ value: 8, done: false }
  32. console.log(iter.next());//{ value: 9, done: false }
  33. console.log(iter.next());//{ value: undefined, done: true }
  34. console.log(iter.next());//{ value: undefined, done: true }
  35. for (let i in obj) {
  36. console.log(i)//start end
  37. }
  38. console.log('------')
  39. for (let i of obj) {
  40. console.log(i)//
  41. /*12, 3, 4, 5,7, 8, 9*/
  42. }

可能失效

12min

  1. var arr = [12, 3, 4, 5, 5];
  2. var iter=arr[Symbol.iterator]();
  3. console.log(iter.next());
  4. console.log(iter.next());
  5. console.log(iter.next());
  6. console.log(iter.next());
  7. console.log(iter.next());
  8. console.log(iter.next());
  9. console.log(iter.next());
  10. for (let i of arr) {
  11. console.log(i);
  12. }
  13. /*两个代码都写,有可能for of不执行了
  14. * 现在是执行的,换个环境可能不执行*/

…不能扩展对象

13.25s

  1. let obj = {
  2. start: [12, 3, 4, 5],
  3. end: [7, 8, 9]
  4. }
  5. let arr=[...obj];//TypeError: obj is not iterable

加入迭代器可以…

14m50s

  1. let obj = {
  2. start: [12, 3, 4, 5],
  3. end: [7, 8, 9],
  4. [Symbol.iterator]() {
  5. let index = 0,
  6. arr = [...this.start, ...this.end],
  7. len = arr.length;
  8. return {
  9. next() {
  10. if (index < len) {
  11. return {
  12. value: arr[index++],
  13. done: false
  14. }
  15. } else {
  16. return {
  17. value: undefined,
  18. done: true
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. let arr=[...obj];//TypeError: obj is not iterable
  26. console.log(arr);
  27. /*[
  28. 12, 3, 4, 5,
  29. 7, 8, 9
  30. ]
  31. */

Array方法

image.png

Array参数

19m.56s

  1. console.log(Array());
  2. console.log(Array(3));//[empty × 3]
  1. console.log(Array());
  2. console.log(Array(3));//[empty × 3]
  3. console.log(Array(3, 1));//[3, 1]
  4. console.log(new Array(3, 1));//[3, 1]
  5. /*new与不new效果一样*/
  6. console.log([3, 1]);//[ 3, 1 ]
  7. /*Array(参数)可能会存在歧义,一般不用这种方式*/

Array of

22m11s

是用来替代Array()的,of()中的内容只会理解成数字,不会产生歧义,理解成长度

  1. console.log(Array.of())//[]
  2. console.log(Array.of(3))//[3]
  3. console.log(Array.of(3, 4, 6))//[3,4,6]

Array from

26m17s

  1. <p>1</p>
  2. <p>2</p>
  3. <p>3</p>
  4. <script !src="">
  5. var oList=document.querySelectorAll('p');
  6. console.log(oList);//NodeList(3)[p, p, p]
  7. /*类数组变成数组,之前的方法*/
  8. console.log([].slice.call(oList));//[p, p, p]
  9. console.log(Array.from(oList));//[p, p, p]
  10. /*对象需要迭代器,才能运行from方法*/
  11. </script>

方法

time 31m50s
Array.from(arrayLike类数组,mapFn?可选,thisArg?)

map

time 33.48

  1. let arr = [1, 2, 3, 4];
  2. let arr1 = arr.map(function (value) {
  3. return value * 2;
  4. })
  5. console.log(arr1);//[ 2, 4, 6, 8 ]
  6. /*map基本用法,遍历数组每一个成员,执行里面匹配的方法,
  7. * 方法可以理解成一个映射关系
  8. * [ 2, 4, 6, 8 ]
  9. * [1, 2, 3, 4]
  10. * 这两个数组,一一对应,映射关系*/

mapFn

time35.14
Array.from(arrayLike类数组,mapFn?可选,thisArg? this指向)
将类数组转换成数组,之后那个类数组,调用map方法,类数组.map(mapFn)

  1. let obj = {
  2. start: [12, 3, 4, 5],
  3. end: [7, 8, 9],
  4. [Symbol.iterator]() {
  5. let index = 0,
  6. arr = [...this.start, ...this.end],
  7. len = arr.length;
  8. return {
  9. next() {
  10. if (index < len) {
  11. return {
  12. value: arr[index++],
  13. done: false
  14. }
  15. } else {
  16. return {
  17. value: undefined,
  18. done: true
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. console.log(Array.from(obj,function mapper(val,idx){
  26. return val*2;
  27. }));
  28. /*[
  29. 24, 6, 8, 10,
  30. 14, 16, 18
  31. ]
  32. */

原型上的方法

time 39.35
之前是构造器上的方法,原型上的方法

[].fill();
copyWithin()
keys()/values()/entries() 这是一组

includes();
find()/findIndex()

fill

time 42.30

  1. let arr1=[1,2,3,4];
  2. var arr=arr1.fill(5);
  3. console.log(arr);//[ 5, 5, 5, 5 ]
  4. /*fill会修改原数组*/
  5. console.log(arr1);//[ 5, 5, 5, 5 ]
  1. let arr1=[1,2,3,4];
  2. arr1.fill(5,0);
  3. /*fill会修改原数组*/
  4. console.log(arr1);//[ 5, 5, 5, 5 ]
  1. let arr1 = [1, 2, 3, 4];
  2. arr1.fill(5,1,2);
  3. /*左闭合,右开[),arr[1]=5,arr[2]没有赋值成5,还是原值*/
  4. console.log(arr1);/*[ 1, 5, 3, 4 ]*/

fill对象

time 54m14s

  1. console.log([].fill.call({length: 3}, 4));
  2. /*{ '0': 4, '1': 4, '2': 4, length: 3 }
  3. * 数组长度为3,填充4进去*/

key、value、entries

Object.key()/Object.values()/Object.entries(0)
time 57m
image.png

array.keys等
time 1h6m2s

  1. let arr = ['a', 'b', 'c'];
  2. console.log(arr.keys());//Array Iterator{}
  3. console.log(arr.values());//Array Iterator{}
  4. console.log(arr.entries());//Array Iterator{}
  5. /*都是返回迭代器对象,遍历器对象,可以用next方法*/
  1. let arr = ['a', 'b', 'c'];
  2. /*console.log(arr.keys());//Array Iterator{}
  3. console.log(arr.values());//Array Iterator{}
  4. console.log(arr.entries());//Array Iterator{}*/
  5. /*都是返回迭代器对象,遍历器对象,可以用next方法*/
  6. var iter=arr.entries();
  7. console.log(iter.next());
  8. console.log(iter.next());
  9. console.log(iter.next());
  10. console.log(iter.next());
  11. console.log(iter.next());

image.png

for of迭代遍历器对象

time 1h9m

  1. let arr = ['a', 'b', 'c'];
  2. var iter = arr.keys();
  3. for (let i of iter) {
  4. console.log(i);//0,1,2
  5. }

遍历器对象length属性

time 1h10m

  1. let arr = ['a', 'b', 'c'];
  2. var iter = arr.keys();
  3. console.log(iter.length);//undefined
  4. for (let i = 0; i < iter.length; i++) {
  5. console.log(i);/*不打印,因为iter.length为undefined*/
  6. }

copyWithin

time 1h13m52s
fill是传入值放入数组,放入的数组的值是外界的
copyWithin是从数组内部拿值,做类似于fill的事情

替换值的用法

arr.copyWithin(target,start,end)
time:1h24m

  1. var arr = [1, 2, 3, 4, 5, 6, 7];
  2. // arr.copyWithin(target,start,end)
  3. console.log(arr.copyWithin(-2, -3, -1));//[1, 2, 3, 4, 5, 5, 6]


image.png
从-2:6开始改变,到结尾,把-3:5,-2:6的值复制,-1:7不算
-2:6变成-2:5,-1:7变成-1:6
可以理解成[-2,-3,-1),只有-1不包含,不会改变原数组长度

call用法

time 1h28m

  1. console.log([].copyWithin.call({length: 5, 3: 1}, 0, 3));//{0: 1, 3: 1, length: 5}
  2. /*改变0,把3:1的值赋值给0:,变成0:1,不会直接填充满了*/
  3. console.log([].copyWithin.call({length: 5, 3: 1, 4: 1}, 0, 3));//{0: 1, 3: 1, length: 5}
  4. console.log([].copyWithin.call({length: 5, 3: 1, 4: 1, 5: 1}, 0, 3));//{0: 1, 1: 1, 3: 1, 4: 1, 5: 1, length: 5}
  5. console.log([].copyWithin.call({length: 5, 3: 1, 4: 1, 5: 1, 6: 1}, 0, 3));//{0: 1, 1: 1, 3: 1, 4: 1, 5: 1, 6: 1, length: 5}

很少用到

find、findIndex

time 1h33m
find解决indexof的不足

  1. var arr = [1, 2, 3, 4];
  2. var arr1=arr.find(function (value, index, arr) {
  3. return value > 2;
  4. })
  5. console.log(arr1);//3
  6. /*因为return value返回,返回符合条件的第一个成员*/
  7. var index=arr.findIndex(function (value, index, arr) {
  8. return value>2;
  9. })
  10. console.log(index);//2

找不到值时

time 1h42m31s

  1. var arr = [1, 2, 3, 4];
  2. var arr1 = arr.find(function (value, index, arr) {
  3. return value > 5;
  4. })
  5. console.log(arr1);//undefined
  6. /*因为return value返回,返回符合条件的第一个成员*/
  7. // var index=arr.findIndex(function (value, index, arr) {
  8. var index = arr.findIndex(function (value) {
  9. return value > 5;
  10. })
  11. console.log(index);//-1

NaN

time 1h44m

  1. console.log([NaN].indexOf(NaN));//-1
  2. console.log([NaN].findIndex(y=>Object.is(NaN,y)));//0
  3. /*通过方法,通过传递自定义方法,弥补indexOf的不足
  4. * 可以用findIndex代替indexOf*/

includes

time 1h48

  1. console.log([1,2,3].includes(3));//true
  2. console.log([1,2,3].includes(4));//false
  3. console.log([1,2,NaN].includes(NaN));//true
  4. /*es6本质上已经解决NaN了,也可以用includes代替indexOf*/

总结

time 1h52m
image.png

数值的扩展

time 1h53m

  1. console.log(0x1f7);//503 16进制
  2. console.log(0o767);//503 8进制
  3. console.log(0O767);//503 8进制
  4. console.log(Number.prototype.toString.call(503,2));
  5. //111110111 转换二进制
  6. console.log(parseInt(111110111,2));//503
  7. console.log(0B111110111);//503

isNAN

time 2h0m

  1. console.log(Number.prototype);

image.png

原本的方法是在全局定义的,可以直接isNaN
es6把以前全局上面的的方法,比如isNaN、parseInt等都定义到了Number上面,Number构造器上面,这是es6的做法之一
这是为了尽少的在全局定义方法
还有进一步修改,一会说

数值的扩展() Number(1新增方法 2方法的调整)

  1. console.log(isNaN(1));//false
  2. console.log(isNaN(NaN));//true
  3. console.log(isNaN('NaN'));//true
  4. console.log(isNaN('NaN1'));//true

time 2h5m

  1. console.log(isNaN('NaN'));//true 隐式转换
  2. console.log(isNaN(NaN));//true
  3. console.log(Number.isNaN('NaN'));//false 修正了
  4. console.log(Number.isNaN(NaN));//true

es6中Number.isNaN解决了isNaN会隐式转换的问题
隐式转换把字符串’NaN’转换成NaN对象

isFinite

time 2h8m

  1. console.log(isFinite('42'));//隐式转换 true
  2. console.log(Number.isFinite('42'));//修正了 false

parseFloat parseInt方法调整

新增方法

isInteger

time 2h13m
认为24.0与24是一样的

  1. console.log(Number.isInteger(24.0));//true
  2. console.log(Number.isInteger(24));//true
  3. console.log(Number.isInteger(24.1));//false

安全整数

time 2h15m
image.png
js可以处理的整数的最大值

一旦数比这更更大,可以解析处理,但就不准了

  1. console.log(Number.MAX_SAFE_INTEGER)//9007199254740991
  2. console.log(Math.pow(2, 53) - 1);//9007199254740991
  3. console.log(Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1);//true
  4. console.log(Number.MIN_SAFE_INTEGER === -Math.pow(2, 53) + 1);//true

判断安全数

time 2h19m50s

  1. console.log(Number.isSafeInteger(1));//true
  2. console.log(Number.isSafeInteger(1.1));//false

Math

time 2h22m52s
比es5更多了,用法简单,考的主要是数学知识
现在这些东西不是特别重要,用到再学

  1. console.log(Math.property);//undefined
  2. console.log(Math)

image.png