[英]Array of objects, with individual properties in javascript
我正在嘗試在javascript中構建對象數組。 不知何故我在任何地方都找不到答案。 是的,我確實搜索過問題。
因此,傳統對象顯然具有以下特性:
項目是對象
item = new Object();
具有屬性和方法
item.name = "sword"; // "sword" being the string name of the object
item.buy = buy; //buy being a function to add said item
那太好了,我明白了。
我也得到數組。
我的問題是,如果我想說其中的20個對象,我該如何將它們制成數組而不是創建許多對象
例如,我知道我可以做到。
item1 = new Object();
item1.name = "sword";
item1.buy = buy;
item2 = new Object();
item2.name = "shield";
item2.buy = buy;
但是,我想做這樣的事情
item = new Array();
item[0].name = "sword";
item[0].buy = buy;
item[1].name = "shield";
item[1].buy = buy;
也許很明顯,但我在這里沒發現問題。
當我嘗試致電
item[0].buy();
我遇到錯誤“未捕獲的TypeError:對象0沒有方法'buy'”,並且item [0] .name未定義。
我在做什么錯,我該怎么辦?
我的猜測是,您想要的是一組對象:
var item = new Array();
item[0] = {};
item[0].name = "sword";
item[0].buy = function() { return this.name };
item[0].buy();
// -> "sword"
// create a new array
var items = [];
// You can push objects onto the array as literals like this
items.push({
name: "sword",
buy: buy
});
// Or you can create the new object as you're doing currently
var item = new Object();
item.name = "shield";
item.buy = buy;
// And push it onto the array as before
items.push(item);
// Now you can access each object by it's index in the array
items[0].buy();
console.log(items[1].name); // "shield"
因為,您沒有在數組的0
索引處創建任何對象,所以buy
應該是一個函數
item = new Array();
// create an object at 0/first index of array which is equivalent of new Object
item[0] = {};
// create property 'name' and assign value ("sword") to it
item[0].name = "sword";
// create another property and assign a function as it's value
// so buy is a method of the first object that is in in the item array
item[0].buy = function(){
return 'buy called';
};
// call 'buy' method from the first object of item array
console.log(item[0].buy());
您可以使用如下文字:
var items = [{
name: '1',
buy: buy
}, {
name: '2',
buy: buy
}, {
name: '3',
buy: buy
}];
但是我會考慮使用原型,因為buy
是一種共享方法:
function Item(name) {
this.name = name;
}
Item.prototype.buy = function() {
...
};
var items = [
new Item('1'),
new Item('2'),
new Item('3')
];
items[1].buy();
可以將語法簡化為:
var arr=[
{name:'foo',age:49},
{name:'GG', age:12},
{name:'MMMM', age:16}
];
一切取決於您的總體目標。
使用var xyz={}
類似於為[]
類似地寫出new Object()
以啟動新數組
您可以創建一個名為Item
的函數,該函數將返回具有name屬性的對象,然后使用prototype
將方法附加到該對象。 盡管JavaScript沒有類,但其行為與之類似。
function Item(name) {
this.name = name
}
Item.prototype.buy = function() {
// your code to buy an item
// you can access your item's name property here by using this.name
alert("You bought the item " + this.name)
}
然后,您可以實例化此函數並將返回的對象添加到數組中:
var items = []
items.push(new Item('sword'))
items.push(new Item('shield'))
items[0].buy() // will buy the item "sword"
items[1].buy() // will but the item "shield"
看起來好像您沒有通過items.push(item1)將項目添加到數組中
item1 = {};
item2 = {};
item1.name = "a";
item2.name = "b";
var buy = function() { console.log('buying... '+this.name); };
item1.buy = buy;
item2.buy = buy;
var items = [];
items.push(item1);
items.push(item2);
items[0].buy();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.