tbapman
tbapman
@Yxiuchao 按照该老哥的思路,0-50生成5个随机数,然后依次50向上累加 ```javascript function solution() { console.time('solution') let hashSet = new Set(); while (true) { if (hashSet.size > 4) { break; } const randomNum = Math.floor(Math.random() * 50); if (hashSet.has(randomNum))...
如果数组中都是都是基本数据类型,那么可以使用 ```javascript function solution(arr){ return Array.from(new Set(arr)) } ``` 现在是数组中的元素可能是引用数据类型,如对象,数组等,为此可以使用Set集合,Set中有的就跳过,没有就插入新数组 ```javascript function solution(arr){ const hashSet=new Set(); const res=[]; for(const i of arr){ if(hashSet.has(JSON.stringify(i))){ continue; } hashSet.add(JSON.stringify(i)); res.push(i) } return res;...
call、apply、bind的区别 作用:改变函数执行时的上下文 为什么要改变执行上下文? a对象又一个方法,由于某种原因b需要调用这个方法,那么b是单独拓展一个方法,还是借用a的方法呢,当然是借用a的方法,因为会减少内存占用 1、cal、apply、bind必须通过函数来调用,第一个参数为对象,若第一个参数为null或者undefined,则指向window 2、call和bind方法可以接收多个参数,apply方法只能接收2个参数,且第二个参数为数组或者伪数组 3、call和apply方法没有返回值(即返回undefined),bind方法返回一个原函数的拷贝,并具有指定的this值和初始参数 4、call和apply方法是立即调用,bind方法是稍后调用 参考:https://segmentfault.com/a/1190000018017796