手写call函数
定义:Function.prototype.call()
let obj = {
name: 123,
myFunc: 'hello'
}
function test() {
console.log(11111, this)
}
Function.prototype.myCall = function(thisObj, ...args) {
// console.log(this)
let Func = this
let s = Symbol('myFunc')
let sArr = Object.getOwnPropertySymbols(thisObj)
delete thisObj[sArr[0]]
console.log('sArr', sArr)
thisObj[s] = Func
thisObj[s](args)
}
// test()
test.myCall(obj, 1)
手写bind函数
定义:Function.prototype.bind()
let obj = {
name: 123,
myFunc: 'hello'
}
function test() {
console.log(11111, this)
}
Function.prototype.myBind = function(thisObj, ...args) {
let Func = this
let s = Symbol('myFunc')
thisObj[s] = Func
return function (){
return thisObj[s].call(thisObj,args);
}
}
fn.myBind (obj,1,2,3)()