Search code examples
javascriptarraysfunctioninheritancefunction-calls

Inheritance / Calling function from object in array


Hello!

I am rather new to Javascript. I am better in Java so I get confused in many things about Javascript. I've created class Clovek witch has some variables and functions. Later I've created array Lide witch has MAX (100) objects of Clovek in it. So when I called later to call function vypis I got only TypeError .. blah blah is not a function. But when i wrote ** console.trace(lide[1])** for example it wrote that is an instance of Clovek. I am confused..

Code here:

function makeid() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for( var i=0; i < 8; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

var odstavec = document.getElementById("test");

function Clovek(vek, pohlavi, jmeno) {
    this.vek = vek;
    this.pohlavi = pohlavi;
    this.jmeno = jmeno;
}

Clovek.prototype.vypis = function() {
    //console.log(this.vek+" "+this.pohlavi+" "+this.jmeno);
    //odstavec.innerHTML = this.vek+" "+this.pohlavi+" "+this.jmeno;
    return this.vek+" "+this.pohlavi+" "+this.jmeno;
};


var MAX = 100; // Maximální počet 

var lide = [MAX];

for( var i = 0; i < MAX; i++) {
    _tempclovek = new Clovek(i,"muz",makeid());
    lide.push(_tempclovek);
    console.log(_tempclovek.vypis());
};

var table = document.createElement("table");
var tbody = document.createElement("tbody");

for (var i = 0; i < MAX; i++) {
    var row = document.createElement("tr");
    var cell = document.createElement("td");
    _tempclovek = lide[i];
    console.trace(_tempclovek.vypis());
    var cellText = document.createTextNode(_tempclovek); 

    cell.appendChild(cellText);
    row.appendChild(cell);
    tbody.appendChild(row);
};


var body = document.getElementsByTagName("body")[0];
table.appendChild(tbody);
body.appendChild(table);
table.setAttribute("border", "2");

EDIT - That code lies inside HTML document im <script> tag


Solution

  • You can create an array with length using new Array(MAX). This will create an array of 100 null values. If you use push, it will append it to array, so use lide[i] instead.

    Code

    function makeid() {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
        for (var i = 0; i < 8; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    
        return text;
    }
    
    var odstavec = document.getElementById("test");
    
    function Clovek(vek, pohlavi, jmeno) {
        this.vek = vek;
        this.pohlavi = pohlavi;
        this.jmeno = jmeno;
    }
    
    Clovek.prototype.vypis = function () {
        //console.log(this.vek+" "+this.pohlavi+" "+this.jmeno);
        //odstavec.innerHTML = this.vek+" "+this.pohlavi+" "+this.jmeno;
        return this.vek + " " + this.pohlavi + " " + this.jmeno;
    };
    
    var MAX = 100; // Maximální počet 
    var lide = new Array(MAX);
    
    for (var i = 0; i < MAX; i++) {
        var _tempclovek = new Clovek(i, "muz", makeid());
        lide[i] = _tempclovek;
        //console.log(_tempclovek.vypis());
    };
    
    
    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    
    for (var i = 0; i < MAX; i++) {
        var row = document.createElement("tr");
        var cell = document.createElement("td");
        var _tempclovek = lide[i];
        console.log("Type Of: " ,typeof(lide[i]))
        console.log(lide)
        console.trace(_tempclovek.vypis());
        var cellText = document.createTextNode(_tempclovek.vypis()); 
    
        cell.appendChild(cellText);
        row.appendChild(cell);
        tbody.appendChild(row);
    };
    
    
    var body = document.getElementsByTagName("body")[0];
    table.appendChild(tbody);
    body.appendChild(table);
    table.setAttribute("border", "2");