手写 call 函数
call 函数就是改变当前函数 this 的指向
思路就是在自己手写的函数内部,在传入的对象中增加同样的函数,然后执行返回,最后删除即可。
1 | const Obj1 = { |
output:
Hello, My name is Sokiy, i’m 20 years old.
Hello, My name is Tong, i’m 18 years old.
手写的 call 函数
不用箭头是因为箭头函数没有自己的this,arguments,super或new.target
我们需要用到这个东西
1 | Function.prototype.tinycall = function (target, ...args) { |
手写 apply 函数
同 call 函数,无非是 args 参数是否是数组的区别