将属性设置为对象的引用

时间:2012-07-12 02:38:53

标签: javascript

我有一个表有一个名为“recordsource”的属性,它将保存将填充表格内容的对象的名称。

<table id="tbl" recordsource="myobj">

现在这是我的职能:

var myobj;

function obj()
{
  this.code = new Array();
  this.name = new Array();
}

myobj = new obj();
myobj.code = ["a","b","c"];
myobj.name = ["apple","banana","carrot"];

function populate_table()
{
  mytable = document.getElementById("tbl");
  mytableobj = mytable.getAttribute("recordsource"); //this will return a string
  //my problem is how to reference the recordsource to the myobj object that have
  //the a,b,c array
}

2 个答案:

答案 0 :(得分:0)

一种方法是使用对象作为您希望能够访问的所有其他对象的列表。

...

var obj_list = {
  'myobj': myobj
};

function populate_table()
{
  mytable = document.getElementById("tbl");
  mytableobj = mytable.getAttribute("recordsource");

  // Then obj_list[mytableobj] == myobj

  obj_list[mytableobj].code[0] // Gives "a"
  obj_list[mytableobj].name[0] // Gives "apple"
}

答案 1 :(得分:0)

试试这个window[ mytableobj ]它会返回myobj