我究竟做错了什么?

时间:2015-03-22 21:40:19

标签: javascript arrays

这是我到目前为止的Javascript代码。我正在尝试使用2个函数创建一个personInfo()对象。我不确定我做错了什么。

var personInfo = 
{
    personInfo.personRelation: " ",
    personInfo.personName: " ",
    personInfo.personAge: 0,
    personInfo.personGender: " "

    var relationValue = personRelation;
    var nameValue = personName; 
    var ageValue = personAge;
    var genderValue = personGender; 

    getPersonInfo: function()               
            {
                document.write( "<p>" + personInfo.personRelation + "<br />" );
                document.write( "Name: " + personInfo.personName + "<br />");
                document.write( "Age: " + personInfo.personAge + "<br />" );
                document.write( "Gender: " + personInfo.personGender + "</p>" );
            }
    setPersonInfo(relationValue, nameValue, ageValue, genderValue): function()
            {
                this.relationValue = relationValue;
                this.nameValue = nameValue;
                this.ageValue = ageValue;
                this.genderValue = genderValue;
            }

}; // end object personInfo

这是我的HTML代码......我试图用while循环打印每个对象。我是Javascript的新手,所以我不确定我是否在外部javascript文件中出错或者它是否只是我的实现。任何帮助都会很棒。谢谢。

<html>
    <head>
        <link rel="stylesheet" href="object.css" type ="text/css" />

        <title>Object Test</title>
        <script type="text/javascript" src="MyObject.js">
            var people = new Array(5);
        </script>
    </head>

    <body>
        <script type="text/javascript" src="MyObject.js">
            var dad_info = setPersonInfo("Dad", "Kenneth Vavrock", 66, "Male");
            dad_info = people[0];
            var mom_info = setPersonInfo("Mom", "Connie Vavrock", 63, "Female");
            mom_info = people[1];
            var brother_info = setPersonInfo("Brother", "Craig Vavrock", 33, "Male");
            brother_info = people[2];
            var nephew_info = setPersonInfo("Nephew", "Sawyer Vavrock", 1, "Male");
            nephew_info = people[3];
            var dad_info = setPersonInfo("Step Mother", "Bonnie Vavrock", 70, "Female");
            stepmother_info = people[4];

            var count = 1;
            while ( count >= 0 )
                {
                    dad_info.getPersonInfo();
                    mom_info.getPersonInfo();
                    brother_info.getPersonInfo();
                    nephew_info.getPersonInfo();
                    stepmother_info.getPersonInfo();
                    count--;
                }
        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:-1)

你的&#34; personInfo&#34;对象格式不正确,看起来像是object literal和类function之间的混合。看看this教程

另外,不要使用空字符串" "作为任何内容的默认值(除非你真的需要),实际上没有任何意义。

解决手头的问题。

var personInfo = function () {
    this.personRelation = "";
    this.personName = "";
    this.personAge = 0;
    this.personGender = "";

    this.getPersonInfo = function () {
        document.write( "<p>" + personInfo.personRelation + "<br />" );
        document.write( "Name: " + personInfo.personName + "<br />");
        document.write( "Age: " + personInfo.personAge + "<br />" );
        document.write( "Gender: " + personInfo.personGender + "</p>" );
    }

};

这种方式可以这样使用:

var people = []; // Create an empty array

// Create new person info for "dad"
var dad_info = new personInfo();
dad_info.personRelation = "...";
// [...]

people.push(dad_info); // Add dad_info to the array.

你应该全力以赴。