Array.prototype.slice = function(start,end){ var result = new Array(); start = start || 0; end = end || this.length; //this指向调用的对象,当用了call后,能够改变this的指向,也就是指向传进来的对象,这是关键 for(var i = start; i < end; i++){ result.push(this[i]); } return result; }
9.每一项设置值
1.终极篇
[1,2,3].fill(false) //[false,false,false]
fill是ES6的方法
2.开始篇
[1,2,3].map(() => 0)
10.每一项是否满足
[1,2,3].every(item=>{return item>2}) //false
every是ES5的api,每一项满足返回 true
11.有一项满足
[1,2,3].some(item=>{return item>2}) //true
some是ES5的api,有一项满足返回 true
12.过滤数组
[1,2,3].filter(item=>{return item>2}) //[3]
filter是ES5的api,返回满足添加的项的数组
13.对象和数组转化
Object.keys({name:'张三',age:14}) //['name','age'] Object.values({name:'张三',age:14}) //['张三',14] Object.entries({name:'张三',age:14}) //[[name,'张三'],[age,14]] Object.fromEntries([name,'张三'],[age,14]) //ES10的api,Chrome不支持 , firebox输出{name:'张三',age:14}