创建对象时交叉引用 js-object 变量
摘要:
我想知道是否可以执行以下操作:
{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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以逐步构建对象:
另一种方法:
You could just build the object progressively:
Another approach: