简体   繁体   中英

Passing Array of Objects from one function to another

I was trying to create a slideshow with css, html and js (w/o jquery or three.js) since i don't know how many elements are going to be in this slideshow i read in all elements of a specific type and then wanted to pass them to the next function for editing the CSS (this way i wouldn't have to catch another exception) but for some, for me unexplainable reason the object is empty after passing it to the write function.

I have tried some different approaches but nothing seemed to work.

Is there a way to pass the array together with the content of the objects?

I have read somewhere that it is because JS creates a copy when passing it. But in the copy the values should also be contained or not?

function read(){
    for (var i = 0;;i++){
        try{
            block.push ({
             id: document.getElementById(i).innerHTML,
              w: window.innerWidth/100*75,
              h: window.innerHeight/100*75, 
           posX: window.innerWidth/3 + i*window.innerWidth/3, 
           posY: window.innerWidth/2});

         console.log("test content: id = " + 
                block[i].id + ", width = " + block[i].w + 
                ", height = " + block[i].h + ", posX = " + 
                block[i].posX + ", posY = " + block[i].posY);       
        }
        catch (err){
            write(block);
            break;
        }
    }
}
function write(){
    for(var i=0; i<block.length; i++){
        console.log(block[i].id);
        //document.getElementById(block[i].id).style.color = "red";
        //document.getElementById(block[i].id).style.position = "absolute";
        //document.getElementById(block[i].id).style.background-position = left     block[i].posX center;
        //document.getElementById(block[i].id).style.width = block[i].w;
   }
}

sry for bad english & bad code. hope it's somewhat understandable.

Example: http://jsfiddle.net/7Y8bB/

Using document.getElementById, if it returns null, I stop the loop, I need <div id="0"></div> to <div id="N"></div>

When I call write, I use write(block).

function read(){
    var el = null;
    var i = 0;
    var block = [];
    do {
        el = document.getElementById(i);
        if (el) {
            block.push ({id: el.innerHTML, w: window.innerWidth/100*75, h: window.innerHeight/100*75, posX: window.innerWidth/3 + i*window.innerWidth/3, posY: window.innerWidth/2});
            console.log("test content: id = " + block[i].id + ", width = " + block[i].w + ", height = " + block[i].h + ", posX = " + block[i].posX + ", posY = " + block[i].posY);      
            i++;
        }
    } while(el);

    write(block);
}

function write(block){
    for(var i=0; i<block.length; i++){
        console.log(block[i].id);
    }
}
read();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM