2016-12-01 1 views
1

私の目的は特別のプロパティをカードに設定することです。私の最初の3つのconsole.logがfalse(デフォルト)を出力しますが、コードの後に​​はtrueを出力します。私はいくつかの方法を試しました(カードの方法を説明してthis.specialをtrueに設定し、後でメソッドを呼び出しました)。 カードを変更するだけです。特別なは機能しませんでした。ありがとう、オブジェクト値を上書きするプロトタイプ値を変更するにはどうすればよいですか?出来ますか?

function card(name,value,special) { 
this.name = name, 
this.value = value, 
this.special = special 
} 
var a9 = new card("nine",9,false); 
var a10 = new card("Ten",9,false); 
var aal = new card("lower",9,false); 

console.log(a9.special); 
console.log(a10.special); 
console.log(aal.special); 
//code comes here 
console.log(a9.special); 
console.log(a10.special); 
console.log(aal.special); 
+0

あなたはa9.special、a10.special、aa1.specialを個別に変更する必要があります。その点は個別に –

+0

です。個別に使用する必要はありません。なぜなら私は30-60種類の異形を持つからです。 – Koppany

+0

60個の変数... ugh ...なぜ 'card'の配列ではないのですか?次にカードを使うことができます。...(...) –

答えて

0

使用プロトタイプspecial

function card(name,value,special) { 
    this.name = name; 
    this.value = value; 
} 

card.prototype.special= false; 

var a9 = new card("nine",9); 
var a10 = new card("Ten",9); 
var aal = new card("lower",9); 

console.log(a9.special); //false 
console.log(a10.special); //false 
console.log(aal.special); //false 

限り、あなたはオブジェクトにspecialを変更しないと、それはprototypeに元の値を指し続けるだろうことに留意すべきであるが、あなたはそれを変更したときに保存します自分のコピーを取得するオブジェクトもあります。 prototype.For例を指していない、もはや:

は、それが自身の独立したコピーで取得しますむしろそれは他の人に影響を与えません、今あなたがtrue

aal.special=true; 
console.log(aal.special);//true 

であるとして、あなたがspecialをする対象の例外カードを持っていると仮定し

console.log(a9.special); //false 
console.log(a10.special); //false 

card.prototype.special= true; // make all true 

console.log(a9.special); //true 
console.log(a10.special); //true 
0

変数をコンソールログに記録し、その値を変更するプロトタイプ関数を作成できます。

コンソールログを呼び出す代わりに、たとえばa9.logToggleSpecial()を呼び出します。すでにこれを試して、それが動作します。

+0

多分私は十分具体的ではありませんでした。私は実際にconsole.logにする必要はありません。私は私のアイデアが機能していたかどうかを見るためにconsole.logを使用しました。 私は、プロトタイプに影響を与え、同時に複数のオブジェクト(a9、a10、aal)のプロパティを変更する関数を必要とします。 – Koppany

0

これは何ですか?

var counter = 0; 
 

 
function Card(name, value, special) { 
 
    this.name = name, 
 
    this.value = value, 
 
    this.special = special; 
 
} 
 

 
defineSpecial(Card.prototype) 
 

 
function defineSpecial(o) { 
 
    var specialValue; 
 
    Object.defineProperty(o, 'special', { 
 
     get() { 
 
      if (counter++ <= 2) { 
 
       return false; 
 
      } 
 
      return specialValue; 
 
     }, 
 
     set(val) { 
 
      specialValue = val; 
 
     } 
 
    }); 
 
} 
 

 
var a9 = new Card("nine",9,true); 
 
var a10 = new Card("Ten",9,true); 
 
var aal = new Card("lower",9,true); 
 
console.log(a9.special); 
 
console.log(a10.special); 
 
console.log(aal.special); 
 
//code comes here 
 
console.log(a9.special); 
 
console.log(a10.special); 
 
console.log(aal.special);

0

あなたは何ができるsomethinglike:

[a9,a10,aal].forEach(function(c) { c.special = true; }); 

あるいは

var cards = { 
    a9: new card("nine",9,false), 
    a10: new card("Ten",9,false), 
    aal: new card("lower",9,false), 
}; 
console.log(cards.a9.special); 
console.log(cards.a10.special); 
console.log(cards.aal.special); 

Object.keys(cards).forEach(function(key) { cards[key].special = true; }); 

console.log(cards.a9.special); 
console.log(cards.a10.special); 
console.log(cards.aal.special); 

それとも少し空想を取得したい場合

var card = function() { 
    var cards = {}; 
    function card(name,value,special) { 
     this.name = name; 
     this.value = value; 
     this.special = special; 
     cards[name] = this; 
    } 
    card.setSpecial = function(b) { 
     Object.keys(cards).forEach(function(key) { 
      cards[key].special = b; 
     }); 
    }; 
    return card; 
}(); 

var a9 = new card("nine",9,false); 
var a10 = new card("Ten",9,false); 
var aal = new card("lower",9,false); 

console.log(a9); 
console.log(a10); 
console.log(aal); 

card.setSpecial(true); 

console.log(a9); 
console.log(a10); 
console.log(aal); 
関連する問題