javascript - 如何正确初始化对象并从头开始?

标签 javascript extjs extjs4

我有这个类(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/

相关文章:

javascript - 我可以稍后调用匿名函数吗

javascript - AngularJS - 从服务访问 Controller 内的元素

javascript - 在组合选择上具有动态显示/隐藏字段的 Extjs 表单

javascript - 网格显示没有数据,但有请求直接为什么

javascript - 如何在 XTemplate 中调用函数 (itemTpl)

javascript - 在不明确询问用户的情况下收集用户时区的最佳方法是什么?

javascript - AngularJS : ng-model doesn't update correctly with ng-keydown. ng-model 关闭一个字符

internet-explorer - ExtJS 和臭名昭著的内存泄漏

javascript - 设置事件过滤器的 Extjs 4.1.3 按钮

javascript - ExtJS 通过关联自动填充模型属性