Object中的对象并分配原型

时间:2015-07-01 07:08:30

标签: javascript html5-canvas

我有以下对象:

var level2blocks = {
    block1 : {
        left : 478,
        right : 515,
        up : 0,
        down : 510
    },
    block2 : {
        left : -10,
        right : 515,
        up : 373,
        down : 600
    }
};

我需要保留对象的名称" block1"和" block2"因为我在其他函数中调用它们,基于html5-canvas中播放器的位置。我也希望将它们保留在对象" level2blocks"因为最后我会有大约20个这样的块。

如果可以分配给block1和block2原型,那么我不必每次都输入所有4个参数?这是我想要使用的原型:

var Block = function (left,right,up,down) {
    this.left = -10;
    this.right = 515;
    this.up = 0;
    this.down = 600;
}

所以我的想法是,我只会传递2个参数,例如只有左和下。缺失值将取自原型值。

例如:

   var level2blocks = {
        block1 : {
            left : 478,
            right : 515,
        },
        block2 : {
            left : -10,
            down : 600
        }
    };

然后在我呼叫level2blocks.block1.down时的功能中,它将引用默认值...

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作。 scrollView.setVisibility(View.INVISIBLE);是内部块的类,Block是用于管理这些块的类。对于内部Level2blocksLevel2blocks可以使用addremove等方法。

在这种情况下,您也可以使用原型值作为默认值,就像我在blocks类中所做的那样。

BlockProto
function extend(obj, props) {
  for (var prop in props) {
    if (props.hasOwnProperty(prop)) {
      obj[prop] = props[prop];
    }
  }
}

function Block(left, right, up, down) {
  this.left = left || -10; // for default values
  this.right = right || 515;
  this.up = up || 0;
  this.down = down || 600;
}

function BlockProto(obj) {
  extend(this, obj);
}

BlockProto.prototype.left = -10;
BlockProto.prototype.right = 515;
BlockProto.prototype.up = 0;
BlockProto.prototype.down = 600;

function Level2blocks() {
  this.blocks = {};
}

Level2blocks.prototype.addBlock = function(left, right, up, down) {
  var numOfBlocks = Object.keys(this.blocks).length;
  this.blocks['block' + numOfBlocks] = new Block(left, right, up, down);
};

Level2blocks.prototype.addPrototypeBlock = function(obj) {
  var numOfBlocks = Object.keys(this.blocks).length;
  this.blocks['block' + numOfBlocks] = new BlockProto(obj);
};

var level2Blocks = new Level2blocks();

level2Blocks.addBlock(2, 4, 2, 4);
level2Blocks.addBlock(123, 2, 090, 24);
level2Blocks.addBlock();
level2Blocks.addBlock(undefined, undefined, undefined, 900);

level2Blocks.addPrototypeBlock({});
level2Blocks.addPrototypeBlock({
  left: 4444
});


console.log(level2Blocks.blocks);
console.log(level2Blocks.blocks.block4.left);
console.log(level2Blocks.blocks.block4.right);

var HTMLStr = '<pre>' + JSON.stringify(level2Blocks.blocks, 0, 4) + '</pre>';

HTMLStr += 'Block4 values from prototype: ';
HTMLStr += [level2Blocks.blocks.block4.right, level2Blocks.blocks.block4.left, level2Blocks.blocks.block4.up, level2Blocks.blocks.block4.down].join(', ');

HTMLStr += '<br/> Block5 values from prototype: ';
HTMLStr += [level2Blocks.blocks.block5.right, level2Blocks.blocks.block5.left, level2Blocks.blocks.block5.up, level2Blocks.blocks.block5.down].join(', ');

document.write(HTMLStr);

答案 1 :(得分:-2)

var level2blocks = {
    block1: new Block(478, 515, 0, 510),
    block2: new Block(-10, 515, 373, 600)
};