简体   繁体   English

将对象数组从一个函数传递到另一个函数

[英]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. 我试图用css,html和js(无jquery或three.js)创建幻灯片,因为我不知道该幻灯片中有多少元素,因此我会阅读特定类型的所有元素,然后想要将它们传递给下一个用于编辑CSS的函数(这样,我就不必捕获另一个异常),但是对于某些原因(由于我无法解释的原因),在将对象传递给write函数后该对象为空。

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. 我在某处读到它是因为JS在传递它时创建了一个副本。 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/ 示例: 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> 使用document.getElementById,如果它返回null,则停止循环,我需要<div id="0"></div><div id="N"></div>

When I call write, I use write(block). 当我调用write时,我使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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