Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我做错了什么?_Javascript_Arrays - Fatal编程技术网

Javascript 我做错了什么?

Javascript 我做错了什么?,javascript,arrays,Javascript,Arrays,这是我到目前为止的Javascript代码。我正在尝试创建一个包含2个函数的personInfo()对象。我不确定我做错了什么 var personInfo = { personInfo.personRelation: " ", personInfo.personName: " ", personInfo.personAge: 0, personInfo.personGender: " " var relationValue = personRelat

这是我到目前为止的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
var personInfo=
{
personInfo.personRelation:“”,
personInfo.personName:“”,
personInfo.personAge:0,
personInfo.personGender:“
var relationValue=个人关系;
var nameValue=personName;
var ageValue=人物;
var genderValue=personGender;
getPersonInfo:function()
{
文件。写(“”+personInfo.personRelation+”
); 文件。书写(“姓名:“+personInfo.personName+”
); 文件。书写(“年龄:“+personInfo.personAge+”
); 文件。写(“性别:“+personInfo.personGender+”

”; } setPersonInfo(relationValue、nameValue、ageValue、genderValue):函数() { this.relationValue=relationValue; this.nameValue=nameValue; this.ageValue=ageValue; this.genderValue=genderValue; } }; // 结束对象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>

对象测试
var-people=新数组(5);
var dad_info=setPersonInfo(“爸爸”,“肯尼思·瓦夫洛克”,66岁,“男性”);
dad_info=人[0];
var mom_info=setPersonInfo(“妈妈”,“康妮·瓦夫洛克”,63岁,“女性”);
妈妈信息=人[1];
var brother_info=setPersonInfo(“兄弟”,“克雷格·瓦夫洛克”,33岁,“男性”);
兄弟信息=人[2];
var nephew_info=setPersonInfo(“nephew”,“Sawyer Vavrock”,1,“男性”);
侄子信息=人[3];
var dad_info=setPersonInfo(“继母”,“邦妮·瓦夫洛克”,70岁,“女性”);
继母信息=人[4];
var计数=1;
而(计数>=0)
{
dad_info.getPersonInfo();
mom_info.getPersonInfo();
brother_info.getPersonInfo();
nephew_info.getPersonInfo();
继母_info.getPersonInfo();
计数--;
}
您的“personInfo”对象的格式不正确,它看起来像是
对象文本
函数
之类的类的混合体。看看教程

另外,不要使用空字符串
作为任何东西的默认值(除非您确实需要),它实际上没有意义

解决眼前的问题

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>" );
    }

};

你应该已经准备好了。

你可以从阅读JS错误控制台开始:
未捕获的语法错误:意外标记。
personInfo。personRelation
作为
personInfo
上的无引号键无效。它应该是
personRelation
<代码>变量在对象文本中无效;所有这些都应该彻底清除
setPersonInfo(relationValue、nameValue、ageValue、genderValue):function()
应该是
setPersonInfo:function(relationValue、nameValue、ageValue、genderValue)
setPersonInfo
personInfo
的一个属性,因此您需要将其称为
personInfo.setPersonInfo()
,尽管这不太有效,由于您试图将其用作构造函数。您确实需要了解代码和之间的区别。请参阅和。您在该代码中至少犯了6种不同类型的错误。你需要退一步,从小处做起。处理让代码一次只做一件事(从头开始)。使用浏览器内置的调试工具。这个问题太广泛了。
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.