构造函数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 是个数组 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);};调用代码 var did = 'dialog1'; //目前layer_custom上的id被覆盖成dialog1了, //但是下次调用alertCustBtn时构造函数上的值就变成dialog1了,能不能让构造函数不记录最近一次的传值,而是在每次调用实例方法,我不覆盖属性,就用构造器上默认的属性值呢? $layer.alertCustBtn(did, '案件详情', ['返回'], '', ['840px', '600px'], (index) => { layer.close(index); }, null, null, () => { _tpl.layuiTplRender('caseDetailPanel_tpl', did, {}); });目前layer_custom上的id被覆盖成dialog1了,但是下次调用alertCustBtn时构造函数上的值就变成dialog1了,能不能让构造函数不记录最近一次的传值,而是在每次调用实例方法,我不覆盖属性,就用构造器上默认的属性值呢?
1 回答
眼眸繁星
TA贡献1873条经验 获得超9个赞
因为你把属性都写在原型链上了
layer_custom.prototype.alertCustBtn = function(){}
这个函数的this相当于layer_custom.prototype;而这个是所有实例共享的,你可以把属性绑定在构造函数上,公用方法放在原型链上
function layer_custom(id, title, Arrbtn, content, area, fn, fn1, fn2, suc, destory){
this.json = {
id: id
//...
}
}
建议看下高程
添加回答
举报
0/150
提交
取消