JavaScript:在返回自定义对象信息时遇到问题

JavaScript: Having trouble returning custom object information

本文关键字:信息 遇到 问题 对象 自定义 返回 JavaScript      更新时间:2023-09-26

我用对象方法制作了一些新对象,我在返回信息时遇到了麻烦。我打算让allPages是一个2d数组:

        var allPages = [[]];
        function textbox(type)
        {
            this.type=type;
            this.getInfo = function () { return ( this.type ); };
        }
        function addTextbox(dropdown)
        {
            var myindex  = dropdown.selectedIndex;
            var SelValue = dropdown.options[myindex].value;
            if(SelValue == "String")
            {
                    var tb = new textbox("string");
                    allPages[allPages.length-1].push(tb);
                    var string = "";
                    for (i = 0;i < allPages.length;i++)
                    {
                        for(j = 0;j < allPages[i].length;j++)
                        {
                            string = string + allPages[i][j].getInfo;
                        }
                    }
                    <!-- Problem here: prints "function () { return this.type; }"-->
                    document.write(string);
                }
            }
}

您不是在调用函数,而是在引用它

allPages[i][j].getInfo;
应该

allPages[i][j].getInfo();

在你声明问题存在的地方上面3行,它应该是:

string = string + allPages[i][j].getInfo(); // mind the () at the end.