将新功能推入数组 - Javascript

时间:2012-09-17 21:30:30

标签: javascript arrays class function

我无法让这个工作:

var global_variables = {
    players: []
}; 

var player = function(properties){
    this.width = properties.width;                            
    this.height = properties.height;
    this.position = properties.position;
    this.position.x = properties.position.x;
    this.position.y = properties.position.y;
}

function load_players(){
    global_variables.players.push(
        [
            new player(
                {
                    width: 10,
                    height: 10,
                    position: {
                        x: 50,
                        y: 50
                    },
                    colour: '#ff0000'
                }
            )
        ]
    );
}


 function init(){ 
    var ctx = load_canvas();
    load_players();

    for(a in global_variables.players){
        var _this = global_variables.players[a];
        alert(_this.colour);
    }
}

alert(_this.colour)只是警告undefined。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

您从undefined获得_this.colour有两个原因:

  1. 您没有在构造函数中设置colour属性
  2. 不是将每个新玩家推入阵列,而是推动包含新玩家的单元素阵列。也就是说,你正在创建一个数组数组。
  3. 将其添加到构造函数:

        this.colour = properties.colour;
    

    然后从load_players()函数中删除方括号:

    function load_players(){
        global_variables.players.push(        
                new player({
                        width: 10,
                        height: 10,
                        position: {
                            x: 50,
                            y: 50
                        },
                        colour: '#ff0000'
                })
        );
    }
    

    演示:http://jsfiddle.net/ZACnC/

答案 1 :(得分:2)

  1. 您在调用init
  2. 之前尝试循环播放
  3. 您正在推送包含在数组中的播放器实例。你只是想推动播放器。
  4. 不要在数组上使用for ... in ...。使用常规循环或像我一样使用forEach
  5. 您实际上从未将colour属性设置为您的实例。
  6. http://jsfiddle.net/GLsR2/

    以下是小提琴的代码:

    var global_variables = {
        players: []
    }; 
    
    var player = function(properties){
        this.width = properties.width;                            
        this.height = properties.height;
        this.position = properties.position;
        this.position.x = properties.position.x;
        this.position.y = properties.position.y;
        this.colour = properties.colour;
    }
    
    function load_players(){
        global_variables.players.push(
            new player(
                    {
                        width: 10,
                        height: 10,
                        position: {
                            x: 50,
                            y: 50
                        },
                        colour: '#ff0000'
                    }
                )
          );
    }
    
    
    init();
    
    global_variables.players.forEach(function(player) {
       alert(player.colour); 
    });
    
    function init(){ 
        load_players();
    }
    

答案 2 :(得分:0)

当您推进global_variables.players时,您正在推送一个包含new Player对象的数组,而不是对象本身。

使用[]时,您不需要.push

function load_players(){
    global_variables.players.push(
        new player({
            width: 10,
            height: 10,
            position: {
                x: 50,
                y: 50
            },
            colour: '#ff0000'
        })
    );
}

P.S。不要将for...in用于数组。只需使用正常的for

for(var a = 0, len = global_variables.players.length; a < len; a++){
    var _this = global_variables.players[a];
    alert(_this.colour);
}

P.P.S。您需要将this.colour = properties.colour;添加到player构造函数中。