Function.prototype.newApply = function (context) {
context = context || window
context.fn = this
let result = arguments[1] ? context.fn(...arguments[1]) : context.fn();
delete context.fn
return result
}
Function.prototype.newBind = function () {
const _this = this;
const args = Array.prototype.slice.call(arguments);
const newThis = args.shift();
return function () {
return _this.newApply(newThis, args)
}
}
Function.prototype.newCall = function (context, ...args) {
context = context || window
context.fn = this
let result = context.fn(...args)
delete context.fn
return result
}
function a(name, password) {
console.log(name, password, this)
return name + password
}
function b() {
console.log("不带参数", this)
return "不带参数的"
}
const aa = a.newCall(b, '徐秋实', '123456')
console.log(aa)
const bb = b.newCall(a)
console.log(bb)
Function.prototype.originCall=function(context){
context = context || window
context.fn=this
const args=Array.prototype.slice.call(arguments);
args.shift()
console.log(args)
const result = context.fn(...args)
delete context.fn
return result
}
Function.prototype.newBindTwo=function(){
const _this=this;
const args=[...arguments];
const newThis=arges.shift();
return function () {
return _this.newApplyTwo(newThis,args)
}
}
Function.prototype.newApplyTwo = function (context) {
context = context || window ;
console.log(context,"---------------context");
context.fn = this;
const result = arguments[1]? context.fn(arguments[1]) : context.fn();
delete context.fn
return result
}
function testApply(name){
return name
}
function testApply2(name){
return name+"/--2"
}
const name = testApply.newApplyTwo(testApply2,'徐秋实')
Function.prototype.newCallTwo = function (context) {
context = context || window ;
context.fn = this;
const args=[...arguments];
args.shift();
const result =context.fn();
delete context.fn;
return result;
}