2013-03-22 54 views
0

您好我似乎有数组深处执行功能的问题:阵列深处执行功能

var Segments = [Segment_1]; 

var Segment_1 = { 
    "requirements": { 
    "Dektec": "True", 
    "SSH": "True" 
    }, 
    "requests": { 
    "R11": "True" 
    }, 
    'function': function(){ 
    print("Hello World!"); 
    } 
}; 


Segments[0]['function'](); 

这个作品,如果我只有一个阵列,虽然深:

var Segments = [Segment_1]; 

function Segment_1() { 
    print("Hello World!"); 
} 

Segments[0](); 

我有在Stackoverflow中看到类似的问题,但都涉及单个级别的数组。任何想法,我在做什么错在这里?我相信这很简单。

感谢

+0

你得到的错误是什么? – Vinay 2013-03-22 17:55:38

+0

Works fine [here](http://jsfiddle.net/EnvXc/),如果你只是将你的数组的定义移动到你试图放入的成员的定义之后。 – 2013-03-22 17:55:54

回答

3

您需要设置Segments设置Segment_1

var Segment_1 = { 
    "requirements": { 
     "Dektec": "True", 
     "SSH": "True" 
    }, 
    "requests": { 
     "R11": "True" 
    }, 
    'function': function(){ 
     document.write("Hello World!"); 
    } 
}; 

var Segments = [Segment_1] 

后,您设置Segments定义Segment_1之前,因此它是不确定的。