2016-11-16 74 views
0
function startMatch(){ 
     Wbrook0 = new component(50, 50, "pieces/WhiteBrook.png", 0, 0, "piece", "Wbro0"); 
     Wknight0 = new component(50, 50, "pieces/WhiteKnight.png", 50, 0, "piece", "Wknight0"); 
     Wbishop0 = new component(50, 50, "pieces/WhiteBishop.png", 100, 0, "piece", "Wbis0"); 
     Wqueen = new component(50, 50, "pieces/WhiteQueen.png", 150, 0, "piece", "Wque"); 
     Wking = new component(50, 50, "pieces/WhiteKing.png", 200, 0, "piece", "Wking"); 
     Wbishop1 = new component(50, 50, "pieces/WhiteBishop.png", 250, 0, "piece", "Wbis1"); 
     Wknight1 = new component(50, 50, "pieces/WhiteKnight.png", 300, 0, "piece", "Wknight1"); 
     Wbrook1 = new component(50, 50, "pieces/WhiteBrook.png", 350, 0, "piece", "Wbro1"); 
    } 

    function helpme(){ 
     console.log(component.length); //This will output 7, which is how many parameters component has 
     console.log(Bking.constructor.length); //Same here 
    } 

    function component(width, height, color, x, y, type, me){ 
     //Mostly unrelated stuff, the stuff I left just defines the parameters 
     this.width = width; 
     this.height = height; 
     this.x = x; 
     this.y = y; 
     this.id = me; 
    } 

是否可以找到所有組件參數的所有當前值? (即如果我插入「x」到代碼,我問到console.log(),它會打印0,50,100,150,200,300和350.是否有可能將值與構造函數的參數進行比較?

我知道我可以爲每一個做console.log(Wbrook.x),但我已經有32個這樣做了,爲了做我想做的事情,我還需要做y和id,並且在我弄糟這個之前,我想我會看看是否有更簡單的方法。

道歉,如果該說明和標題是模糊的,我沒有與構造多少經驗呢。先謝謝了。

+0

這是我的看法:https://jsfiddle.net/sfwhtqwy/ –

回答

1

我會保存組件在實例化數組時,可以查詢該數組並選擇所需的屬性

_allComponents = []; 
 

 
function startMatch() { 
 
    Wbrook0 = new component(50, 50, "pieces/WhiteBrook.png", 0, 0, "piece", "Wbro0"); 
 
    Wknight0 = new component(50, 50, "pieces/WhiteKnight.png", 50, 0, "piece", "Wknight0"); 
 
    Wbishop0 = new component(50, 50, "pieces/WhiteBishop.png", 100, 0, "piece", "Wbis0"); 
 
    Wqueen = new component(50, 50, "pieces/WhiteQueen.png", 150, 0, "piece", "Wque"); 
 
    Wking = new component(50, 50, "pieces/WhiteKing.png", 200, 0, "piece", "Wking"); 
 
    Wbishop1 = new component(50, 50, "pieces/WhiteBishop.png", 250, 0, "piece", "Wbis1"); 
 
    Wknight1 = new component(50, 50, "pieces/WhiteKnight.png", 300, 0, "piece", "Wknight1"); 
 
    Wbrook1 = new component(50, 50, "pieces/WhiteBrook.png", 350, 0, "piece", "Wbro1"); 
 
} 
 

 
function helpme() { 
 
    console.log(component.length); //This will output 7, which is how many parameters component has 
 
    console.log(Bking.constructor.length); //Same here 
 
} 
 

 
function component(width, height, color, x, y, type, me) { //Mostly unrelated stuff, the stuff I left just defines the parameters 
 
    this.width = width; 
 
    this.height = height; 
 
    this.x = x; 
 
    this.y = y; 
 
    this.id = me; 
 
    _allComponents.push(this); 
 
} 
 

 
function getPropertyOfComponents(components, propertyName) { 
 
    var values = []; 
 
    components.forEach(function(component) { 
 
    values.push(component[propertyName]); 
 
    }); 
 
    return values.join(","); 
 
} 
 

 
startMatch(); 
 
console.log(getPropertyOfComponents(_allComponents, 'x'));

相關問題