innerHTML没有输出任何东西

时间:2017-01-07 15:52:36

标签: javascript oop prototype

我是编写代码的整个OOP JavaScript风格的新手,所以我正在努力学习它以适应潮流。

我写了一个简单的函数,我试图利用原型和function Person(name, age, height, weight, IQ, ethnicity){ this.fullName = name; this.age = age; this.height = height; this.weight = weight; this.IQ = IQ; this.ethnicity = ethnicity; } let Jou = new Person("Joseph", 21, 191, 78, 131, "caucasian"); let Franc = new Person("Francois", 13, 178, 50, 125, "caucasian"); let Svata = new Person("Svatopluk", 61, 188, 85, 140, "caucasian"); let names= ["Jou", "Franc", "Svata"]; let text = ""; for (let i = 0; i < names.lenght; i++){ text += names[i].fullName + " is " + names[i].age + " years old, " + names[i].height + "cm tall, weighs approximately " + names[i].weight + "kg, his IQ is around " + names[i].IQ + " and he belongs to the " + names[i].ethnicity.toUpperCase() + " ethnicity." + "<br>" ; } document.getElementById("name").innerHTML = text; 。看下面是:

<p id="name"></p>

我将其插入:df = pd.DataFrame({'ID':[1,1,2,2,3,3], 'YEAR' : [2011,2012,2012,2013,2013,2014], 'V': [0,1,1,0,1,0], 'C':[00,11,22,33,44,55]})

任何人都可以给我一些指示,关于我缺少什么,所以我可以学习如何使用它?

谢谢, Ĵ

2 个答案:

答案 0 :(得分:2)

names数组包含字符串,而不是实际变量。你应该删除双引号。此外,names.lenght应为names.length

&#13;
&#13;
function Person(name, age, height, weight, IQ, ethnicity){
    this.fullName = name;
    this.age = age;
    this.height = height;
    this.weight = weight;
    this.IQ = IQ;
    this.ethnicity = ethnicity;
}

let Jou = new Person("Joseph", 21, 191, 78, 131, "caucasian");
let Franc = new Person("Francois", 13, 178, 50, 125, "caucasian");
let Svata = new Person("Svatopluk", 61, 188, 85, 140, "caucasian");

let names= [Jou, Franc, Svata];
let text = "";
for (let i = 0; i < names.length; i++){
        text += names[i].fullName + " is " + names[i].age + " years old, " 
             + names[i].height + "cm tall, weighs approximately " 
             + names[i].weight + "kg, his IQ is around " + names[i].IQ + 
             " and he belongs to the " + names[i].ethnicity.toUpperCase() + 
             " ethnicity." + "<br>" ;
}
document.getElementById("name").innerHTML = text;
&#13;
<div id="name"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你已经在字符串中包含了变量名,而你应该将它们作为变量引用,而不是字符串,所以改变它:

let names= ["Jou", "Franc", "Svata"];

到此:

let names= [Jou, Franc, Svata];

此外,你有一个拼写错误,这就是你的原始let names= [Jou, Franc, Svata];无效的原因,所以改变

names.lenght
         ^^^

names.length
         ^^^