2010-06-14 72 views
1

摘要:

我想知道是否有可能做這樣的事情:通過使用一些其他指針一樣{a: 'A',b: self.a}交叉引用的JS對象變量當創建對象

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

...或{a: 'A',b: own.a}或其他任何東西......

全部問題:

我試圖用Ext.extend延長MyBaseModule,並且需要交叉參考值在傳遞給Ext.extend()的擴展對象中。

由於我還沒有使用MyModule,因此我無法使用這個來引用對象(請參見第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); 
+0

我意識到對象尚未創建時,我想訪問它的屬性,還等什麼我正在尋找的是JavaScript語言中的某種內置幫助功能,以告知它在創建對象後解析引用... 嗯......我看起來越多在此情況下,我認爲這種功能存在的可能性越小。 – 2010-06-14 12:15:31

回答

1

你可以只建立逐步對象:

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 
    }] 
}; 
+0

我想沒有其他辦法了。我希望避免像這樣分解對象定義,但我可能只是難以... – 2010-06-14 12:46:41