我想要做的是弄清楚如何创建一个传递对象参数的函数调用。在我下面的代码中,我创建了一个对象数组。我只是想弄清楚如何创建一个传递新对象的参数的函数调用。传递的参数将是现有对象中的相同数据项。这是我的代码:
(function(){
console.log('******Objects of Student Information Below!******');
//Student Information in An Object
var studentInfo=[{name1: 'SabrinaHill', address1:{address1:'172 Brushcreek Dr', city1:'Sanford',state1:'FL'},gpa1:[3.2,3.7,4.0]},{name2: 'JoelOsteen', address2:{address2:'3226 Lilac Dr', city2:'Portsmouth',state2:'VA'},gpa2:[3.0,2.7,2.0]},{name3: 'JoelOsteen', address3:{address3:'3226 Lilac Dr', city3:'Portsmouth',state3:'VA'},gpa3:[3.0,2.7,2.0]}];
console.log('Name:',studentInfo[0].name1);
console.log('Address:',studentInfo[0].address1.address1,studentInfo[0].address1.city1,studentInfo[0].address1.state1);
console.log('GPA:',studentInfo[0].gpa1[0]+',',studentInfo[0].gpa1[1]+',',studentInfo[0].gpa1[2]);
console.log('Name:',studentInfo[1].name2);
console.log('Address:',studentInfo[1].address2.address2,studentInfo[1].address2.city2,studentInfo[1].address2.state2);
console.log('GPA:',studentInfo[1].gpa2[1]+',',studentInfo[1].gpa2[1]+',',studentInfo[1].gpa2[1]);
console.log('******Added Information******');
var args = ('super man', '123 Test Dr', 'Orlando', 'Florida' [3.2, 4.0, 2.2]);
console.log('Name:',studentInfo[0].name1);
console.log('Address:',studentInfo[0].address1.address1,studentInfo[0].address1.city1,studentInfo[0].address1.state1);
console.log('GPA:',studentInfo[0].gpa1[0]+',',studentInfo[0].gpa1[1]+',',studentInfo[0].gpa1[2]);
console.log('Name:',studentInfo[1].name2);
console.log('Address:',studentInfo[1].address2.address2,studentInfo[1].address2.city2,studentInfo[1].address2.state2);
console.log('GPA:',studentInfo[1].gpa2[1]+',',studentInfo[1].gpa2[1]+',',studentInfo[1].gpa2[1]);
})();
我正在尝试让控制台显示使用函数添加的新信息。任何帮助将不胜感激。
由于
我也在寻找有关如何在按钮上创建onclick事件的帮助,当点击该按钮时,调用一个函数,该函数使用JavaScript的innerHTML属性在网站的HTML框中显示所有对象数据(一次一个学生)
以下是我的HTML:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Student Info</title>
<meta name="description" content="">
<meta name="author" content="">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="form_box">
<div id="contact-form">
<p class="heading">Display Students Information Below:</p>
<div id="form-box">
<div id="output">
<div id="name">
<p></p>
</div>
<div id="address">
<p></p>
</div>
<div id="gpa">
<p></p>
</div>
<div id="date">
<p></p>
</div>
<div id="gpaavg">
<p></p>
</div>
<div id="phone">
<p></p>
</div>
<!-- <div class="clear"></div> -->
</div>
<div id="info_box">
<div id="info_btn">
<h4 id="round" class="heading">Click To See Next Student</h4>
<a href="#" class="buttonred">Next</a>
</div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
答案 0 :(得分:0)
取出属性名称的编号,例如name1应该只是名称等 然后制作如下函数:
function modifyStudent(studentObj,name,address,city,state,gpa){
studentObj.name=name;
studentObj.address = {
address:address,
city:city,
state:state
};
studentObj.gpa = gpa;
}
然后使用student对象和名称地址等参数调用该函数
var studentInfo = [{
name:"Test",
address:{
address:"No where",
city:"Chicago",
state:"IL",
},
gpa:[]
}];
modifyStudent(studentInfo[0],'super man', '123 Test Dr', 'Orlando', 'Florida', [3.2, 4.0, 2.2]);
console.log(studentInfo[0]);
由于javascript是通过对象和数组的引用传递的,当函数修改了对象属性时,在studentInfo[0]
对象之后使用它时,这些更改将是可见的。
或者您可以创建一个具有修改学生数据的方法的学生对象
function student(){
this.name="";
this.address={address:"",city:"",state:""};
this.gpa=[];
this.modifyStudent(name,address,city,state,gpa){
this.name=name;
this.address = {
address:address,
city:city,
state:state
};
this.gpa = gpa;
};
}
var studentInfo= [new student(),new student()];
console.log(studentInfo[0]);
studentInfo[0].modifyStudent('super man', '123 Test Dr', 'Orlando', 'Florida', [3.2, 4.0, 2.2]);