아날로그 콜, apply, bind

6309 단어 전면 면접
Function.prototype.mycall=function(context,...args){
    context=context||null;
    context.fn=this;//this 
    let res=context.fn(...args);// context 
    delete context.fn;
    return res;
}

Function.prototype.myapply=function(context,args){
    context=context||null;
    context.fn=this;//this 
    let res=context.fn(...args);// context 
    delete context.fn;
    return res;
}

Function.prototype.mybind=function(context,...args){
    context=context||null;
    let fn=this;//this 
	let fbind=function(...arg_s){
		// new 
		if(this instance of fn)return fn.call(context,...args,...arg_s)
		// , ,this , 
		// 
		return fn.call(this,...args,...arg_s)
	}
	let fp=function(){};
	fp.prototype=this.prototype;
	fbind.prototype=new fp();
	return fbind;
}

좋은 웹페이지 즐겨찾기