创建对象时交叉引用 js-object 变量

发布于 2024-09-05 18:13:40 字数 1387 浏览 2 评论 0原文

摘要:

我想知道是否可以执行以下操作:

{a: 'A',b: this.a}

...通过使用其他指针,例如 {a: 'A',b: self.a}{ a: 'A',b: own.a} 或其他任何...

完整问题:

我正在尝试使用 Ext.extend 扩展 MyBaseModule,并且需要交叉引用传递给的扩展对象中的值Ext.extend()。

由于我尚未进入 MyModule 上下文,因此无法使用 this 来引用该对象(请参阅第 12 行下面的示例)。有没有其他方法可以在不先创建对象的情况下引用这样的值?

1 MyModule = Ext.extend(MyBaseModule, {
2   dataStores: {
3       myDataStore: new Ext.data.Store({...})
4   },
5
6   myGridDefinition: {
7       id:                 'myGridDefinitionPanel',
8       bodyBorder:         false,
9       items: [{
10          xtype:          'grid',
11          id:             'myGridDefinitionGrid',
12          store:          this.dataStores.myDataStore
13      }]
14  }
15 });

或者以下是唯一的解决方案? 如果可能的话,我想避免这种情况,因为我发现它对于大型扩展定义来说可读性较差。

1 var extensionObject = {
2   dataStores: {
3       myDataStore: new Ext.data.Store({...})
4   },
5
6   myGridDefinition: {
7       id:                 'myGridDefinitionPanel',
8       bodyBorder:         false,
9       items: [{
10          xtype:          'grid',
11          id:             'myGridDefinitionGrid'
12      }]
13  }
14 };
15
16 extensionObject.locationsGrid.items[0].store = extensionObject.dataStores.locations;
17
18 MyModule = Ext.extend(MyBaseModule, extensionObject);

Summary:

I want to know if it is possible to do something like this:

{a: 'A',b: this.a}

...by using some other pointer like {a: 'A',b: self.a} or {a: 'A',b: own.a} or anything else...

Full question:

I'm trying to extend MyBaseModule using Ext.extend, and need to cross-reference values in the extension object passed to Ext.extend().

Since I'm not yet in context of MyModule, I'm not able to use this to reference the object (See example below line 12). Is there any other way to reference values like this without creating the object first?

1 MyModule = Ext.extend(MyBaseModule, {
2   dataStores: {
3       myDataStore: new Ext.data.Store({...})
4   },
5
6   myGridDefinition: {
7       id:                 'myGridDefinitionPanel',
8       bodyBorder:         false,
9       items: [{
10          xtype:          'grid',
11          id:             'myGridDefinitionGrid',
12          store:          this.dataStores.myDataStore
13      }]
14  }
15 });

Or is the following the only solution?
I would like to avoid this if possible, as I find it less readable for large extension definitions.

1 var extensionObject = {
2   dataStores: {
3       myDataStore: new Ext.data.Store({...})
4   },
5
6   myGridDefinition: {
7       id:                 'myGridDefinitionPanel',
8       bodyBorder:         false,
9       items: [{
10          xtype:          'grid',
11          id:             'myGridDefinitionGrid'
12      }]
13  }
14 };
15
16 extensionObject.locationsGrid.items[0].store = extensionObject.dataStores.locations;
17
18 MyModule = Ext.extend(MyBaseModule, extensionObject);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风几经秋 2024-09-12 18:13:40

您可以逐步构建对象:

var dataStores = {
    myDataStore: new Ext.data.Store({...})
};

var extensionObject = {
    dataStores: dataStores,
    myGridDefinition: {
        id: 'myGridDefinitionPanel',
        bodyBorder: false,
        items: [{
            xtype: 'grid',
            id: 'myGridDefinitionGrid',
            store: dataStores.myDataStore
        }]
    }
};

另一种方法:

var extensionObject = {
    dataStores: {
        myDataStore: new Ext.data.Store({...})
    }
};

extensionObject.myGridDefinition = {
    id: 'myGridDefinitionPanel',
    bodyBorder: false,
    items: [{
        xtype: 'grid',
        id: 'myGridDefinitionGrid',
        store: extensionObject.dataStores.myDataStore
    }]
};

You could just build the object progressively:

var dataStores = {
    myDataStore: new Ext.data.Store({...})
};

var extensionObject = {
    dataStores: dataStores,
    myGridDefinition: {
        id: 'myGridDefinitionPanel',
        bodyBorder: false,
        items: [{
            xtype: 'grid',
            id: 'myGridDefinitionGrid',
            store: dataStores.myDataStore
        }]
    }
};

Another approach:

var extensionObject = {
    dataStores: {
        myDataStore: new Ext.data.Store({...})
    }
};

extensionObject.myGridDefinition = {
    id: 'myGridDefinitionPanel',
    bodyBorder: false,
    items: [{
        xtype: 'grid',
        id: 'myGridDefinitionGrid',
        store: extensionObject.dataStores.myDataStore
    }]
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文