js里面function参数

k = T.ajaxForm({
            dataType: "json",
            data: n(),
            timeout: 3e5,
            crossDomain: !0,
            xhrFields: {
                withCredentials: !0
            },
            beforeSubmit: function(e, t, n) {},
            uploadProgress: function(e, t, n, r) {
                p(".upload__percent").text(r + "%")
            },
            success: function(e, t) {
                l(e)
            },

这段代码里面有三个类似beforeSubmit: function(e, t, n) {},的json格式,我调试时候发现不同function里面的e的值是不一样,这是怎么做到的呢
js地址是
http://shared.ydstatic.com/fa...
网址是http://fanyi.youdao.com/

阅读 6.9k
6 个回答

这里的e是函数形参标识而已,当然会依据传入函数的实参变化而不同啊(调用时机可能不同)。
这里之所以都用e作为形参标识,是因为对应的都是event形式的实参(猜测)而做的简化(毕竟这样的形参其实是对用户透明的)

就是函数呀,给它不同的值,当然不一样了

钩子函数 在不同的时候调不同的方法 传不同的值啊 类似

function test(obj){
        if(obj.before){
            obj.before({a:1});
        }
        setTimeout(obj.after || Function.prototype,1000,{c:2})
    }
    test({
        before:function(obj){
            console.log(obj);
        },
        after:function(obj){
            console.log(obj);
        }
    })

这个。。函数参数你想起什么名字起什么名字,名字一样不代表值就一样,值只跟调用函数传入有关,参数名只是别名

function test(a){console.log(a)}
test(1);//1

function test(b){console.log(b)}
test(1);//1

function test(a){console.log(a)}
test(2);//2

回调函数

function test(options){
  option.beofreSetTimeout(3,2,1);
  setTimeout(function(){
    option.callback(1,2,3)
  },1000)
}

test({
  beofreSetTimeout:function(e,b,r){
    console.log(e,b,r);//3,2,1
  },
  callback:function(e,b,r){
    console.log(e,b,r);//1,2,3
  }
})

方法里面定义的,可以看下源码里面是怎么定义的

你需要了解下 T.ajaxForm 这个函数,以下是我模拟的他的内部构造:

T.ajaxForm = (function(){
    function AjaxForm(option){
        if (!(this instanceof AjaxForm)) {
            return new AjaxForm(option);
        }
        
        this._beforeSubmit = option['beforeSumbit'];
        this._uploadProgress = option['uploadProgress'];
        this._success = option['success'];
        
        this._run();
    }
    
    AjaxForm.prototype = {
        constructor: AjaxForm , 
        
        _initialize: function(){
            this._xhr = new XMLHttpRequest();
        } ,  
        
        // 开启 xhr
        _open: function(method , url , isAsync , username , password){
            this._xhr.open(method , url , isAsync , username , password);
        } , 
        
        // 设置响应头
        _setHeader: function(k , v){
            this._xhr.setRequestHeader(k , v);
        } ,
        
        // 定义相关事件
        _defineEvent: function(){
            this._xhr.upload.onprogress = function(event){     
                // 表单提交之后调用
                if (typeof this._uploadProgress=== 'function') {
                    // t 应该是 event.total
                    // n 应该是 event.loaded
                    // r 应该是 event.loaded / event.total
                    this._uploadProgress.call(this , event , t , n , r);
                }
            };
            
            this._xhr.onreadystatechang = function(event){
                if (this.readyState === 4&& this.status === 200) {
                    if (typeof this._success === 'function') {
                        // 猜测:event
                        // 猜测:this.responseText
                        // 请求收到服务器响应后触发!
                        this._success.call(this , event , this.responseText);
                    }
                }
            };
        } , 
        
        // 发送数据
        _send: function(data){
            // 表单提交之前调用
            if (typeof this._beforeSubmit === 'function') {
                // e 、t、n 由你是用 T.ajaxForm 的具体实现定义!!
                // 我这边只是假设 ....
                this._beforeSubmit.call(this , e , t , n);
            }
            
            this._xhr.send(data);
        } , 
        
        _run: function(){
            
        }
    };
    
    return AjaxForm;
})();

这样应该很清晰明了的知道为什么

T.ajaxForm({
    ....
    beforeSubmit: function(e, t, n) {},
    uploadProgress: function(e, t, n, r) {
        p(".upload__percent").text(r + "%")
    },
    success: function(e, t) {
        l(e)
    },
    .....

他们的参数不同了吧:因为每一个回调函数在满足条件触发条件时 T.ajaxForm 使用了不同的参数进行调用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题