2013-10-28 21 views
0

我想在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(); 

我遇到錯誤「遺漏的類型錯誤:對象0有沒有方法‘團購’」和項目[0]。名稱是不確定的。

我在做什麼錯,我該怎麼做呢?

回答

3

我的猜測是,你想要的是對象的數組:

var item = new Array(); 
item[0] = {}; 
item[0].name = "sword"; 
item[0].buy = function() { return this.name }; 
item[0].buy(); 
// -> "sword" 
+0

謝謝!在發佈問題後大約2分鐘,我想到了這一點。我以爲所有東西都是一個對象,那個數組元素也是如此,但並沒有意識到它們只是在初始化時纔會這樣。 –

2
// 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

這也是一個很好的解決方案。 雖然我喜歡我的代碼儘可能簡潔。 感謝您的輸入! –

0

因爲,你沒有在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()); 
0

您可以使用文字是這樣的:

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

可以簡化語法到:

var arr=[ 
    {name:'foo',age:49}, 
    {name:'GG', age:12}, 
    {name:'MMMM', age:16} 
]; 

一切都取決於你的總體目標是什麼。

使用var xyz={}是一樣的,僑胞,寫出new Object()[]開始一個新的陣列

0

您可以創建一個名爲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" 
0

看起來好像您沒有通過items.push將商品加入您的陣列(物品1)

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(); 
相關問題