call,apply和bind的内部实现

call

函数都可以调用call,apply,bind方法,所以函数方法要写在原型上
        Function.prototype.ca_ll=function(){
            let [thisArg,...arg]=arguments;
            //thisArg=window;
            thisArg.func=this;
            var result=thisArg.func(...arg);                //内部参数指向thisArg
            delete thisArg.func;
            return result;
        }
        var obj={
            name:"sily"
        }
        function fn(arg1,arg2){
            console.log(this.name);
            console.log(arg1,arg2);
            
        }
        fn.ca_ll(obj,1,2);

apply

Function.prototype.app_ly=function(){
                var [thisArg,args]=arguments;
                thisArg.func=this;
                var result;
                // if(!args){
                //     result=thisArg.func();
                // }else{
                    result&#

你可能感兴趣的:(js基础,call,apply,bind,js,bind,prototype)