我有这个类(class):
Ext.define('Customer_Portal_UI.FormParameters', {
constructor: function (c) {
Ext.each(this.ParticipantFormParameters, function (field) {
if (field.toFieldSet != null) {
var fieldSetName = c.targetFieldSetPrefix + field.toFieldSet;
field.toFieldSet = fieldSetName;
}
});
},
ParticipantFormParameters: [
{ name: 'service_executive_account_Number', visible: false },
{ name: 'firstname', visible: true, displayText: 'Firstname', toFieldSet: 'GeneralInfoFS', allowBlank: false },
{ name: 'lastname', visible: true, displayText: 'Lastname', toFieldSet: 'GeneralInfoFS', allowBlank: false },
{ name: 'gendercode', visible: true, hasCheckbox: true, displayText: 'Gender', toFieldSet: 'GeneralInfoFS', xtype: 'combo', allowBlank: false },
]
});
如果我这样做一次:
var formParams = Ext.create(Customer_Portal_UI.FormParameters, { targetFieldSetPrefix: 'add' });
console.log(formParams.ParticipantFormParameters[0].toFieldSet);
我将得到addGeneralInfoFS
。
但是如果我多次创建这个对象,我会得到 addGeneralInfoFS, addaddGeneralInfoFS, addaddaddGeneralInfoFS
这是怎么回事?我尝试删除 formParams
但当我初始化一个对象时,如果再次使用相同的类型,则会出现同样的问题。
是 ExtJS 还是 JavaScript 问题?我觉得很奇怪,我需要付出努力来简单地摧毁物体......
编辑:啊!我在 documentation 中找到了这个:
When placing items in the prototype of the prototype, they will be shared across all instances of the class. Unless you specifically want to do this, you should only put "primitive" types inside the prototype definition.
现在的问题是如何声明成员而不让它们“在原型(prototype)中”?很好,但是 ParticipantFormParameters
到底去哪里了呢?我不希望它出现在配置中,因为它很脏,每次初始化对象时,我都必须拖动这个巨大的(真正的代码在数组中有更多项目)JavaScript 标记 block ?
我已经用这种方法重构了我的应用程序中的内容,但在它起作用之前我完全瘫痪了......我无法取消重构......
谢谢。
最佳答案
为什么要修改属性?使用两个不同的变量。这可能是解决方案:
(...)
Ext.each(this.ParticipantFormParameters, function (field) {
if (field.toFieldSet != null) {
var fieldSetName = c.targetFieldSetPrefix + field.toFieldSetRaw;
field.toFieldSet = fieldSetName;
}
});
},
ParticipantFormParameters: [
{ name: 'service_executive_account_Number', visible: false },
{ name: 'firstname', visible: true, displayText: 'Firstname', toFieldSetRaw: 'GeneralInfoFS', allowBlank: false },
关于javascript - 如何正确初始化对象并从头开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25319971/