如何更改会覆盖对象值的原型值?有可能吗?

时间:2016-12-01 08:44:24

标签: javascript oop prototype

所以我的目标是设置特殊属性。在某种程度上,我的前三个console.log将打印为false(默认情况下),但在代码之后它将打印为true。我尝试了几种方法(在中描述一种方法,将 this.special 设置为true,然后调用该方法。没有工作)。也只是更改 card.special 不起作用。谢谢,

    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);

4 个答案:

答案 0 :(得分:0)

您可以创建一个原型函数来控制记录变量并更改其值。

card.prototype.logToggleSpecial = function() {
  console.log(this.special);
  if(!this.special)
    this.special = !this.special;
};

而不是调用控制台日志,只需调用例如a9.logToggleSpecial()。已经尝试过这个并且有效。

答案 1 :(得分: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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你可以做点什么:

[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);

答案 3 :(得分: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中的原始值,但当你在某个对象上更改它时获得自己的副本。不再指向原型。例如:

现在假设您有一张例外卡,您希望specialtrue

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

它不会影响他人,而是获得它自己的独立副本

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