Beginner [removed] Working with JSON and Objects in JavaScript

前端 未结 4 1892
失恋的感觉
失恋的感觉 2020-12-31 19:31

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

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

I want

4条回答
  •  时光取名叫无心
    2020-12-31 19:57

    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
    

提交回复
热议问题