0
我有一个JavaScript“模块”访问项目(我们称他们在python)在一个名为thing.js:不能在JavaScript关联数组
function Thing(){
this.a = "foo";
this.b = "bar";
this.c = "";
this.d = function(){
this.c = this.a + this.b;
};
};
var things = {
'one':Thing(),
'two':Thing(),
'three':Thing()
};
alert(things["one"]); // output: undefined!!??
/* Pick out two random items from things. */
var random_thing = function(){
//var grabbed = 0;
//while(grabbed < 1){
var pick = getRandomInt(0,2);
alert(things["one"]); // output: undefined!!
//if()
//return pick;
};
的代码是有点不完整的,我想随机挑选两件东西并返回。但这不是直接的问题。
我有一个单独的 “主” javascript文件名为main.js,这就要求对这些对象和功能:
$div1.append(random_thing());
在我的html文件,我包括JavaScript文件:
但我不断收到的输出是“未定义”的警报(东西['一'])!我不明白第一个警报如何返回undefined,它正好在关联数组的定义之下。