var arr = [1,2,3,4,5] console.log(arr.slice(1,4)) console.log(arr) Function.prototype.bind1 = function(){ // arguments是個串列不是陣列,將引數拆解為陣列 const args = Array.prototype.slice.call(arguments) // 獲取this(陣列第一項),shift方法是洗掉第一項回傳第一項值 const t = args.shift() // fn1.bind(...)中的fn1 const self = this //回傳一個函式 return function(){ return self.apply(t,args) } } function fn(a,b){ console.log(this) console.log(a,b) return 'ok' } const fn2 = fn.bind1({x:100},10,20) console.log(fn2())
考點:
使用閉包和理解作用域
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/11184.html
標籤:JavaScript
上一篇:前端面試題整理——作用域和閉包
下一篇:前端面試題整理——深拷貝
