在函数中获取此对象名称

时间:2013-12-04 16:11:29

标签: javascript jquery html

这是我的JavaScript:

function drinkable(name, price, effect, heal, alcohol){
    this.name = name;
    this.price = price;
    this.effect = effect;
    this.heal = heal;
    this.alcohol = alcohol;
    this.use = function(position){
        satisfy(thirst,this.effect,"#thirst");
        inventory.splice(position, 1); updateInventory();
        alcoholLevel = alcoholLevel + this.alcohol; updateAlcohol();
        changeHP(this.heal); checkHP();
    };
};
// DRINKABLES - name, price, thirst, HP, alcohol
var d1 = new drinkable("Bottled Water", 0.84, 15, 0, 0);
var d2 = new drinkable("Raush Juice", 0.90, 15, 1, 0);
var d3 = new drinkable("Vodka Alosuth", 2.70, 5, -15, 1.2);

但我对我创建的新功能有疑问:

function store(name, description, goods){
    this.name = name;
    this.description = description;
    this.goods = goods;
    this.listGoods = function(){
        var foo = "<p>"+this.name+": "+this.description+"</p><table class='table table-condensed'><tr><th>item</th><th>price</th><th></th></tr>";
        for (var i=0;i<this.goods.length;i++){
            foo = foo + ("<tr><td>"+this.goods[i].name+"</td><td>"+this.goods[i].price+"</td><td><a class='buy' onClick='buyToInventory("+this.goods[i]+")</a></td></tr>"); // this line is a problem
        }
        foo = foo + "</table>";
        return foo;
    }
}
// STORES - name, description, goods
var s1 = new store(
    "24/7 Store",
    "All day, all night, every day!",
    [d1,d2,d3]
);

上面这个函数中的所有内容都有效:

+"</td><td><a class='buy' onClick='buyToInventory("+this.goods[i]+")</a></td></tr>"

这是创造它的目的,例如:

<a class='buy' onClick='buyToInventory(d1)'>buy</a>

但它会返回:

<a class='buy' onClick='buyToInventory([object Object])'>buy</a>

如何强制它返回变量名而不是整个对象?

3 个答案:

答案 0 :(得分:0)

this.goods [i]是一个对象。 object.toString函数显示以下内容。你想要的是对象变量本身

+"</td><td><a class='buy' onClick='buyToInventory(s1.goods["+i+"])</a></td></tr>"

你想要s1对象的货物数组中的第i项。 更好的想法是使用不显眼的javascript来创建元素并添加事件监听器(或处理程序)。

当onClick事件触发时,它不知道上下文是什么。所以你需要引用它,好像它是从全局范围调用

答案 1 :(得分:0)

我在构造函数中添加了ID,如下所示:

function drinkable(ID, name, price, effect, heal, alcohol){
    this.ID=ID;
    this.name = name;
    this.price = price;
    this.effect = effect;
    this.heal = heal;
    this.alcohol = alcohol;
    this.use = function(position){
        satisfy(thirst,this.effect,"#thirst");
        inventory.splice(position, 1); updateInventory();
        alcoholLevel = alcoholLevel + this.alcohol; updateAlcohol();
        changeHP(this.heal); checkHP();
    };
};

然后我还要更新所有对象

// DRINKABLES - name, price, thirst, HP, alcohol
var d1 = new drinkable("d1", "Bottled Water", 0.84, 15, 0, 0);
var d2 = new drinkable("d2", "Raush Juice", 0.90, 15, 1, 0);
var d3 = new drinkable("d3", "Vodka Alosuth", 2.70, 5, -15, 1.2);

现在我刚刚将<td><a class='buy' onClick='buyToInventory("+this.goods[i]+")</a></td>改写为<td><a class='buy' onClick='buyToInventory("+this.goods[i].ID+")</a></td>

答案 2 :(得分:0)

不幸的是,在函数范围内,您无法获取用于将值传递给函数的变量的名称。有关详情,请参阅此帖子:how to get object's name in javascript?