Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
103
rated 0 times [  110] [ 7]  / answers: 1 / hits: 23667  / 15 Years ago, fri, may 15, 2009, 12:00:00

I have some JSON returned to the browser like this product:



{ Title: School Bag, Image: /images/school-bag.jpg }


I want this data to be a Product object so I can use prototype methods like a toHTMLImage() that returns a HTML image representation of the product:



function Product() { }
Product.prototype.toHTMLImage = function() { //Returns something like <img src=<Image> alt=<Title> /> }


How do I convert my JSON results into a Product object so that I can use toHTMLImage?


More From » json

 Answers
39

Simple, if I got it,



var json = { Title: School Bag, Image: /images/school-bag.jpg }
function Product(json) {
this.img = document.createElement('img');
this.img.alt = json.Title;
this.img.src = json.Image;

this.toHTMLImage = function() {
return this.img;
}
}

var obj = new Product(json); // this is your object =D

[#99530] Monday, May 11, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tye

Total Points: 415
Total Questions: 103
Total Answers: 116

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;