2017-02-22 32 views
0

我有一个很大的JS对象,看起来像这样(截断)node.js的阅读自我价值 - 出口对象

var argData = module.exports = { 
    SetName: { 
    definitions: [ 
     { name: 'clientMacAddress', alias: 'c', type: String }, 
     { name: 'name', alias: 'n', type: String } 
    ], 
    usage: [ 
     { 
      header: 'blah', 
      content: 'blah' 
     }, 
     { 
      header: 'Options', 
      optionList: [ 
       { name: 'clientMacAddress', alias: 'c', type: String }, 
       { name: 'name', alias: 'n', type: String } 
      ] 
     } 
    ] 
} 
... 

正如你所看到的,一切都在argData [“SETNAME”]定义为optionList部分。我想只在optionsList段的一个变量子,所以它看起来是这样的:

... 
header: 'Options', 
     //already tried doing this and it didn't work 
     optionList: argData["SetLatency"].definitions 
    } 
... 

我想引用一个关键的对象中本身(如果是有道理的)。我试过“self.argData”,但没有奏效。我在想,因为我在这里宣布它,所以不可能做一个变量,但我想确保。如果不是,那么处理这个问题的最好方法是什么?出口前:

回答

0

把公共代码的变量(键.i.e):

const keys = [ 
       { name: 'clientMacAddress', alias: 'c', type: String }, 
       { name: 'name', alias: 'n', type: String } 
      ]; 

var argData = module.exports = { 
    SetName: { 
    definitions: keys, 
    usage: [ 
     { 
      header: 'blah', 
      content: 'blah' 
     }, 
     { 
      header: 'Options', 
      optionList: keys 
     } 
    ] 
} 
... 
+0

这工作,谢谢! –