对象方法不会以 HTML 格式打印出来。有谁知道为什么下面的代码不起作用?
function person(firstname, lastname, age, eyecolor)
{
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.eyecolor = eyecolor;
this.printing = function print(){
id = for(var x = 0;x < person.length; x++){
document.getElementById('main_div').innerHTML = person[x];
}
};
}
var myFather = new person("John", "Doe", 50, "blue");
var myMother = new person("Sally", "Rally", 48, "green");
myFather.printing;
最佳答案
几个错误。
1) id = for(var x = 0;x < person.length; x++){
没有任何意义。您不能将 for 循环的值分配给变量。所以只需删除 id =
部分。
2) 您没有调用您的函数。因此将最后一行更改为 myFather.printing()
.
编辑。您还可以更改 this.printing = function print(){
到只是this.printing = function() {
因为名字print
没有在任何地方使用。
关于javascript - 不会打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23034552/