Note:
var Product = function()
{
this.Name;
}
When you do an ajax call to get a .json file, it maps it directly to a loose javascript object, Let's imagine:
products.json contains:
[
{
"Name": "Tool"
},
{
"Name": "Sausage"
},
];
My ajax call gets the contents of that and of course it's pushed straight into a javascript array of objects:
var products = [
{
Name: "Tool"
},
{
Name: "Sausage"
},
];
prototype much?
I know I can create a function that takes the product i'm on and deals with the stuff there:
function WorkOutBulkDiscount( product )
{
// Do stuff
}
But I was wondering If I can get the syntax sugar from prototyping:
Product.prototype.WorkOutBulkDiscount = WorkOutBulkDiscount;
for( var a in products )
{
var bulkDiscount = products[ a ].WorkOutBulkDiscount();
// Do something with it
}
Does XML allow you to declare what type it should be mapped into for javascript?
The end result would allow me to use prototyping!
The only other way of doing this seems to be to create a new array of new Product() and do the mapping to it... Which is obviously something i'm not going to do just for a bit of candy.
No comments:
Post a Comment