从另一个创建一个新对象并从一个数组中读取它。 (JavaScript)的

时间:2022-09-25 09:09:58

I have the following example constructor:

我有以下示例构造函数:

function Loot(type, sellValue) {
    this.type = type;
    this.sellValue = sellValue; 
}

I am looking to inherit these values into other objects, which are then pushed into an array, such as:

我希望将这些值继承到其他对象中,然后将其推送到数组中,例如:

var inventory = [];

var stone = new Loot("craft", 20);
inventory.push(stone);

var hat = new Loot("clothing", 80);
inventory.push(hat);

var coal = new Loot("ore", 5);
inventory.push(coal);

var diamond = new Loot("ore", 400);
inventory.push(diamond);


console.log(inventory);

However, when I do so, my inventory reads as (Loot, Loot, Loot, Loot) and not the names given to the items, (stone, hat, coal, diamond).

但是,当我这样做时,我的库存读作(Loot,Loot,Loot,Loot)而不是给予物品的名称(石头,帽子,煤炭,钻石)。

How can I get around this? I imagine it would require some form of inheritance?

我怎么能绕过这个?我想它需要某种形式的继承?

Thanks!

谢谢!

3 个解决方案

#1


0  

However, when I do so, my inventory reads as (Loot, Loot, Loot, Loot) and not the names given to the items, (stone, hat, coal, diamond).

但是,当我这样做时,我的库存读作(Loot,Loot,Loot,Loot)而不是给予物品的名称(石头,帽子,煤炭,钻石)。

You would need to iterate your inventory array and log the .type properties of all items:

您需要迭代库存数组并记录所有项目的.type属性:

for (var i=0; i<inventory.length; i++)
    console.log(inventory[i].type);

Or you'd build right another array from the item types, which could most easily be accomplished with the map method:

或者你从项目类型构建另一个数组,这可以通过map方法最容易地完成:

var types = inventory.map(function(item) { return item.type; });
console.log(types);

#2


2  

The variable names like stone do not mean anything to the object. They are a reference to your object, but not the data of the object.

像石头这样的变量名称对于对象没有任何意义。它们是对象的引用,但不是对象的数据。

So stone is simply a variable name for the same new Loot that was created.

因此,stone只是创建的同一个新Loot的变量名。

You have only one Loot object in your example.

您的示例中只有一个Loot对象。

Also, if you look in your array with a debugger i will show the type of object (Loot). You need to expand that to see the values inside the object.

此外,如果您使用调试器查看数组,我将显示对象的类型(Loot)。您需要展开它以查看对象内的值。

#3


-1  

The array can not have string keys. Apparently you need to use an object to store these objects.

该数组不能有字符串键。显然,您需要使用对象来存储这些对象。

function Loot(type, sellValue){
  this.type = type;
  this.sellValue = sellValue; 
}

var inventory = {};
var inventory2 = {};

var stone = new Loot("craft", 20);
var stone1 = new Loot("craft1", 30);
var stone2 = new Loot("craft2", 40);

//or as follows: 
inventory2['stone'] = stone;
inventory2['stone1'] = stone1;
inventory2['stone2'] = stone2;

console.log(inventory);   // Object {craft: Loot, craft1: Loot, craft2: Loot}
console.log(inventory2);  // Object {stone: Loot, stone1: Loot, stone2: Loot}

http://jsbin.com/aNiXolo/5/edit

http://jsbin.com/aNiXolo/5/edit

#1


0  

However, when I do so, my inventory reads as (Loot, Loot, Loot, Loot) and not the names given to the items, (stone, hat, coal, diamond).

但是,当我这样做时,我的库存读作(Loot,Loot,Loot,Loot)而不是给予物品的名称(石头,帽子,煤炭,钻石)。

You would need to iterate your inventory array and log the .type properties of all items:

您需要迭代库存数组并记录所有项目的.type属性:

for (var i=0; i<inventory.length; i++)
    console.log(inventory[i].type);

Or you'd build right another array from the item types, which could most easily be accomplished with the map method:

或者你从项目类型构建另一个数组,这可以通过map方法最容易地完成:

var types = inventory.map(function(item) { return item.type; });
console.log(types);

#2


2  

The variable names like stone do not mean anything to the object. They are a reference to your object, but not the data of the object.

像石头这样的变量名称对于对象没有任何意义。它们是对象的引用,但不是对象的数据。

So stone is simply a variable name for the same new Loot that was created.

因此,stone只是创建的同一个新Loot的变量名。

You have only one Loot object in your example.

您的示例中只有一个Loot对象。

Also, if you look in your array with a debugger i will show the type of object (Loot). You need to expand that to see the values inside the object.

此外,如果您使用调试器查看数组,我将显示对象的类型(Loot)。您需要展开它以查看对象内的值。

#3


-1  

The array can not have string keys. Apparently you need to use an object to store these objects.

该数组不能有字符串键。显然,您需要使用对象来存储这些对象。

function Loot(type, sellValue){
  this.type = type;
  this.sellValue = sellValue; 
}

var inventory = {};
var inventory2 = {};

var stone = new Loot("craft", 20);
var stone1 = new Loot("craft1", 30);
var stone2 = new Loot("craft2", 40);

//or as follows: 
inventory2['stone'] = stone;
inventory2['stone1'] = stone1;
inventory2['stone2'] = stone2;

console.log(inventory);   // Object {craft: Loot, craft1: Loot, craft2: Loot}
console.log(inventory2);  // Object {stone: Loot, stone1: Loot, stone2: Loot}

http://jsbin.com/aNiXolo/5/edit

http://jsbin.com/aNiXolo/5/edit