Extjs merge objects

后端 未结 3 653
挽巷
挽巷 2021-02-06 05:08

I have a super class in my application, that defines an object like this:

Ext.define(\'superclass\', {
    myObject: {
        prop1: true,
        prop2: 200,
          


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 05:53

    @Molecular man's answer is correct, however I would use the merge function.

    Lets say you have this in your first config:

    myObject: {
       prop1: false,
       person: {
           name: "John"
       }
    }
    

    And your second object

    myObject: {
       prop2: false,
       person: {
           surname: "Doe"
       }
    }
    

    Your apply will overwrite person and you'll only have person.surname.

    Into something like this:

    myObject: {
       prop1: false,
       prop2: false,
       person: {
           surname: "Doe"
       }
    }
    

    Merge will merge the two objects.

    Ext.define('childclass', {
        extend: 'superclass',
    
        constructor: function() {
            this.myObject = Ext.merge(this.myObject, {
                prop3: false,
                prop4: false
            });
            this.callParent(arguments);
        }
    });
    

    Note that this will only be needed in the sitation as described in my example, if not you can use Molecular's answer.

提交回复
热议问题