// 手写call函数function call(Fn,obj,...arg){ // 如果obj为null或者undefined,则指向window if(obj === undefined || obj === null){ // globalThis是ES11的新特性,指向全局 obj = globalThis } //为obj添加临时方法 obj.temp = Fn // 调用 temp 方法 let result = obj.temp(...arg) // 删除obj 的 temp delete obj.temp return result}
function add(a,b){ console.log(this); return a + b + this.c}let obj = { c:521}globalThis.c = 1314console.log(call(add,obj,10,20)); //551console.log(call(add,null,10,20)); //1344
// 手写call函数Function.prototype.call = function(obj,...arg){ // 如果obj为null或者undefined,则指向window if(obj === undefined || obj === null){ // globalThis是ES11的新特性,指向全局 obj = globalThis } //为obj添加临时方法 obj.temp = this // 调用 temp 方法 let result = obj.temp(...arg) // 删除obj 的 temp delete obj.temp return result}function add(a,b){ console.log(this); return a + b + this.c}let obj = { c:521}globalThis.c = 1314console.log(add.call(obj,10,20)); //551console.log(add.call(null,10,20)); //1344
// 手写apply函数Function.prototype.apply = function(obj,arg){ if(obj === null || obj === undefined){ obj = globalThis } obj.temp = this let result = obj.temp(...arg) delete obj.temp return result}function add(a,b){ console.log(a+b+this.c);}let obj = { c:1314}globalThis.c = 520add.apply(obj,[10,20]) //1344add.apply(null,[10,20]) //550
function bind(Fn,obj,...args){ if(obj === null || obj === undefined){ obj = globalThis } //bind返回一个函数,调用的时候执行方法 return function(...args2){ // 调用call方法 obj.temp = Fn let result = obj.temp(...args,...args2) delete obj.temp return result }}function add(a,b){ console.log(arguments); console.log(a+b+this.c);}let obj = { c:1314}globalThis.c = 520bind(add,obj,10,20)() //1344bind(add,null,10,20)(30,40) //550
<script> window.addEventListener(‘scroll‘,throttle(function(){ console.log(Date.now()); },500)) function throttle(callback,wait){ // 定义开始时间 let start = 0 // 返回结果是一个函数 return function(e){ let now = Date.now() if(now - start > wait){ callback.call(this,e) start = now } } }</script>
<body> <input type="text"> <script> let input = document.querySelector(‘input‘) // input.onkeydown = function(e){ // console.log(e.keyCode); // } input.onkeydown = debounce(function(e){ console.log(e.keyCode); },1000) function debounce(callback,time){ //定时器变量 let timeId = null // 返回值一定是个函数,否则无法触发回调 return function(e){ //timeId !== null 证明已经有一个timeif在跑,先清除掉再继续跑 if(timeId !== null){ clearTimeout(timeId) } //启动定时器 timeId = setTimeout(()=>{ callback.call(this,e) // 重置定时器变量 timeId = null },time)
} }</script></body>
const arr = [1,2,3,4,5]Array.prototype.map = function (callback) { let result = [] for(let i = 0;i<this.length;i++){ result.push(callback(this[i],i)) } return result}let arr2 = arr.map((item,index) => { return item *10})console.log(arr2);
const arr = [1,2,3,4,5]// 示例let result = arr.reduce((res,value)=>{ return res + value},0) //0为res初始值,value为arr的值console.log(result); //15
Array.prototype.reduce = function(callback,value){ let result = value for(let i = 0;i<this.length;i++){ result = callback(result,this[i]) } return result}// 演示let arr2 = arr.reduce((res,value)=>{ return res + value},5)console.log(arr2);
const arr = [1,2,3,4,5]Array.prototype.filter2 = function(callback){ let arr = [] for(let i = 0;i<this.length;i++){ if(callback(this[i],i)){ arr.push(this[i]) } } return arr}let res = arr.filter2((item=>{ return item > 2}))console.log(res);
// findIndex()Array.prototype.findIndex2 = function(callback){ for(let i = 0;i<this.length;i++){ if(callback(this[i],i)){ return i } } return -1}let res = arr.findIndex2((item=>{ return item > 1000}))console.log(res);
const arr = [1,2,3,4,5]Array.prototype.every2 = function(callback){ for(let i = 0;i<this.length;i++){ let result = callback(this[i],i) if(!result){ return false; } } return true}const result = arr.every2(item=>{ return item > 0})console.log(result);
如果你想用Python开辟副业赚钱,但不熟悉爬虫与反爬虫技术,没有接单途径,也缺乏兼职经验
关注下方微信公众号:Python编程学习圈,获取价值999元全套Python入门到进阶的学习资料以及教程,还有Python技术交流群一起交流学习哦。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!