Array.from函数的第一个参数

Array.from函数的第一个参数是一个带有length属性的对象的时候

Array-from.js

//first demo
require("@fatso83/mini-mocha").install();
const { expect } = require('chai');
describe('Array.from test',()=>{
  it('array-like test',()=>{
    let arrResult = Array.from([0,1,2,3,4],(ele,index)=>{
      return ele *2;
    });
    expect(arrResult).to.deep.equal([0,2,4,6,8]);
  });
  
  it('object with length prop test',()=>{
    let arrResult = Array.from({length:5},(ele,index)=>{
      return index *2;
    });
    expect(arrResult).to.deep.equal([0,2,4,6,8]);
  });
  //为什么这里能调用一个包含length熟悉的对象呢?因为它其实是一个类数组对象,只不过它的每个值都是undefined
});

版权声明:著作权归作者所有。

thumb_up 0 | star_outline 0 | textsms 0