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

鍍金池/ 問答/HTML/ 每次給實例方法傳入不同的值,能不能不覆蓋構(gòu)造函數(shù)上屬性的值?

每次給實例方法傳入不同的值,能不能不覆蓋構(gòu)造函數(shù)上屬性的值?

構(gòu)造函數(shù)
function layer_custom() {
    this.json = {
        type: '',
        title: null,
        content: '',
        area: '',
        //skin:'layui-layer-rim',
        //offset:
        btnAlign: 'c',
        closeBtn: null,
        // shade: 0,
        shade: [.1, '#000'],
        shadeClose: false,
        time: 0,
        id: 'onlyOne',//這個id屬性不想被覆蓋
        anim: 0,
        resize: false,
        yes: null,
        no: null,
        cancel: null,
        btn1: null,
        btn2: null,
        btn3: null
    }
}
實例方法
// 最多可傳入3個自定義按鈕名稱
layer_custom.prototype.alertCustBtn = function(id, title, Arrbtn, content, area, fn, fn1, fn2, suc, destory) {
    this.json.type = 1;
    this.json.id = id;
    this.json.btn = Arrbtn; //Arrbtn 是個數(shù)組
    this.json.title = title;
    this.json.closeBtn = 1;
    this.json.content = content;
    this.json.area = area;
    this.json.btn1 = function(index, layero) {
        fn && fn(index);
        // layer.close(index);
    };
    this.json.btn2 = function(index) {
        fn1 && fn1(index);
        // layer.close(index);
    };
    this.json.btn3 = function(index) {
        fn2 && fn2(index);
        // layer.close(index);
    };
    this.json.success = function() {
        suc && suc();
    }
    this.json.end = function() {
        destory && destory();
    }
    layer.open(this.json);
};
調(diào)用代碼
 var did = 'dialog1';
   //目前l(fā)ayer_custom上的id被覆蓋成dialog1了,
   //但是下次調(diào)用alertCustBtn時構(gòu)造函數(shù)上的值就變成dialog1了,能不能讓構(gòu)造函數(shù)不記錄最近一次的傳值,而是在每次調(diào)用實例方法,我不覆蓋屬性,就用構(gòu)造器上默認的屬性值呢?

    $layer.alertCustBtn(did, '案件詳情', ['返回'], '', ['840px', '600px'], (index) => {
        layer.close(index);
    }, null, null, () => {
        _tpl.layuiTplRender('caseDetailPanel_tpl', did, {});
    });
目前l(fā)ayer_custom上的id被覆蓋成dialog1了,
但是下次調(diào)用alertCustBtn時構(gòu)造函數(shù)上的值就變成dialog1了,能不能讓構(gòu)造函數(shù)不記錄最近一次的傳值,而是在每次調(diào)用實例方法,我不覆蓋屬性,就用構(gòu)造器上默認的屬性值呢?
回答
編輯回答
裸橙

因為你把屬性都寫在原型鏈上了
layer_custom.prototype.alertCustBtn = function(){}
這個函數(shù)的this相當于layer_custom.prototype;而這個是所有實例共享的,你可以把屬性綁定在構(gòu)造函數(shù)上,公用方法放在原型鏈上

function layer_custom(id, title, Arrbtn, content, area, fn, fn1, fn2, suc, destory){

this.json = {
    id: id
    //...
}

}

建議看下高程

2018年8月9日 15:05