Reputation: 3
hello there my second question here ive been trying to create a clicker started over from scratch and this time using design patterns but ive been stuck on this :
var Store_Item = {
name: '',
amount: 0,
cost: {
Costs: 0
/* could ask different stuff here */
},
Get: {
GetAmount: 0
}
};
Store_Item = $.makeArray(Store_Item);
Store_Item[1] = new Store_Item {Name:"name", amount:0};
But i want to call Cost or Get like this :
Store_Item[1] = new Store_Item {Name: "name", amount:0, cost:{Costs: 3}};
Like that
Upvotes: 0
Views: 60
Reputation: 5148
I think you don't understand the concept of Instantiable Classes in JavaScript...
On you code you create an object, and you try to create multi instance of it.. But you can, you just can use this one.
If you want do that, you must set your object like that :
// Define
var myObject = function(arg) {
this.myProperty = arg;
this.myMethode = function () {
return this.myProperty;
}
}
// Init
var object_a = new myObject("a");
var object_b = new myObject("b");
// Use
alert( object_a.myMethode() );
In your case:
// Define object
var Stored_item = function(name, amount) {
this._name = name;
this._amount = amount;
this._costs = [];
this.add_cost = function(cost){
this._costs.push(cost);
}
}
// Create array list
var stored_items = [];
// Add objects on array
stored_items.push( new Stored_item("name A", 100) );
stored_items.push( new Stored_item("name B", 550) );
// Using first object (with "name A" name)
stored_items[0].add_cost(52);
Upvotes: 2
Reputation: 32831
I'm not sure I understand what you're trying to do, but if the statement:
Store_Item = $.makeArray(Store_Item);
was meant to create an array with a single element, it should be:
Store_Item = [Store_Item];
$.makeArray has a completely different purpose.
Now if the "statement":
Store_Item[1] = new Store_Item {Name:"name", amount:0};
Is meant to append a copy of the first object with some properties changed, this will do the job:
Store_Item[1] = $.extend({}, Store_Item[0], {name:"name", amount:0});
etc...
Is this of any help?
Upvotes: 0