sách gpt4 ăn đã đi

javascript - 初级 JavaScript : Working with JSON and Objects in JavaScript

In lại 作者:数据小太阳 更新时间:2023-10-29 04:00:51 28 4
mua khóa gpt4 giày nike

我有一些像这个“产品”一样返回给浏览器的 JSON:

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

我希望此数据成为“产品”对象,因此我可以使用原型(prototype)方法,如返回产品的 HTML 图像表示的 toHTMLImage():

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

如何将我的 JSON 结果转换为 Product 对象,以便我可以使用 toHTMLImage?

câu trả lời hay nhất

简单,如果我明白了,

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

关于javascript - 初级 JavaScript : Working with JSON and Objects in JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/866482/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress