var name = 'windowName'
var a = {
name:'cherry',
fn:function(a,b){
console.log('name',this.name)
},
fn1:function(a,b){
console.log(a+b);
}
}
var b = a.fn1;
b.call(a,1,2)
b.apply(a,[1,2])
b.bind(a,1,2)()
Function.prototype.mycall = function(ctx){
ctx = ctx || window;
ctx._fn = this;
let arg = [...arguments].slice(1);
let result = ctx._fn(...arg);
delete ctx._fn;
return result
}
Function.prototype.myCall = function(ctx,...arg){
ctx = ctx || window;
ctx._fn = this
let result = ctx._fn(...arg)
delete ctx._fn
return result
}
Function.prototype.myApply = function(ctx,arg){
ctx = ctx || window;
ctx._fn = this
let result = ctx._fn(...arg)
delete ctx._fn
return result
}
Function.prototype.myBind = function(ctx){
let that = this;
ctx = ctx || window;
let arg = [...arguments].slice(1)
return function(){
that.apply(ctx,arg.concat(...arguments))
}
}
Function.prototype.newBind = function (that) {
if (typeof this !== 'function') {
throw new TypeError('Error')
}
const args = [...arguments].slice(1);
const fn = this;
return function () {
fn.apply(that)
};
return function F() {
if (this instanceof F) {
return new fn(...args, ...arguments)
}
return fn.apply(that, args.concat(...arguments))
}
};