在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ js里面function參數(shù)

js里面function參數(shù)

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格式,我調(diào)試時候發(fā)現(xiàn)不同function里面的e的值是不一樣,這是怎么做到的呢
js地址是
http://shared.ydstatic.com/fa...
網(wǎng)址是http://fanyi.youdao.com/

回答
編輯回答
玄鳥

你需要了解下 T.ajaxForm 這個函數(shù),以下是我模擬的他的內(nèi)部構(gòu)造:

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);
        } , 
        
        // 設(shè)置響應(yīng)頭
        _setHeader: function(k , v){
            this._xhr.setRequestHeader(k , v);
        } ,
        
        // 定義相關(guān)事件
        _defineEvent: function(){
            this._xhr.upload.onprogress = function(event){     
                // 表單提交之后調(diào)用
                if (typeof this._uploadProgress=== 'function') {
                    // t 應(yīng)該是 event.total
                    // n 應(yīng)該是 event.loaded
                    // r 應(yīng)該是 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
                        // 請求收到服務(wù)器響應(yīng)后觸發(fā)!
                        this._success.call(this , event , this.responseText);
                    }
                }
            };
        } , 
        
        // 發(fā)送數(shù)據(jù)
        _send: function(data){
            // 表單提交之前調(diào)用
            if (typeof this._beforeSubmit === 'function') {
                // e 、t、n 由你是用 T.ajaxForm 的具體實現(xiàn)定義??!
                // 我這邊只是假設(shè) ....
                this._beforeSubmit.call(this , e , t , n);
            }
            
            this._xhr.send(data);
        } , 
        
        _run: function(){
            
        }
    };
    
    return AjaxForm;
})();

這樣應(yīng)該很清晰明了的知道為什么

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

他們的參數(shù)不同了吧:因為每一個回調(diào)函數(shù)在滿足條件觸發(fā)條件時 T.ajaxForm 使用了不同的參數(shù)進行調(diào)用

2017年10月8日 18:50
編輯回答
祉小皓

就是函數(shù)呀,給它不同的值,當(dāng)然不一樣了

2018年1月31日 07:30
編輯回答
陌璃

方法里面定義的,可以看下源碼里面是怎么定義的

2018年1月5日 18:07
編輯回答
逗婦惱

這個。。函數(shù)參數(shù)你想起什么名字起什么名字,名字一樣不代表值就一樣,值只跟調(diào)用函數(shù)傳入有關(guān),參數(shù)名只是別名

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

回調(diào)函數(shù)

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
  }
})
2018年7月26日 16:51
編輯回答
笑浮塵

這里的e是函數(shù)形參標(biāo)識而已,當(dāng)然會依據(jù)傳入函數(shù)的實參變化而不同?。ㄕ{(diào)用時機可能不同)。
這里之所以都用e作為形參標(biāo)識,是因為對應(yīng)的都是event形式的實參(猜測)而做的簡化(畢竟這樣的形參其實是對用戶透明的)

2017年3月9日 15:36
編輯回答
青裙

鉤子函數(shù) 在不同的時候調(diào)不同的方法 傳不同的值啊 類似

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);
        }
    })
2017年2月27日 04:00