2016-11-16 8 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; 
    } 

コンポーネントのすべてのパラメータの現在の値をすべて見つけることはできますか? (つまり、私がconsole.log()に尋ねているコードに "x"を差し込むと、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'));

関連する問題