我不知道如何创建一个管理货物的类。1. 按价格排序 2. 按名称排序我创建了一个创建对象的构造函数。class Products { constructor(name,price,description,img){ this.name = name; this.price = price; this.description = description; this.img = img; } } var nike = new Products("Nike", 100, "new-shoes","img/nike.png"); var adidas = new Products("Adidas", 120, "classic-shoes","img/adidas.png"); var puma = new Products("Puma",150,"new-shoes","img/puma.png"); var jordan = new Products("Jordan", 170, "outlet-shoes", "img/jordan.png"); var converse = new Products("Converse",70,"outlet-shoes","img/convrse.png") var nikeAirMax = new Products("Nike Air Max", 200, "shoes","img/nikeAirMax.png"); var newBal = new Products("New Balance 990",179,"new-shoes","img/newBal.png"); var arrGoods = [nike,adidas,puma,jordan,nikeAirMax,converse,newBal];然后创建了一个函数,在 HTML 文件中显示商品。 function addGoods(item){ for (let i = 0; i<arrGoods.length; i++){ document.getElementById("products").innerHTML += `<div class="info-goods"> <div class="img"><img src=${item[i].img}></div> <div class="name">${item[i].name}</div> <div class="price">${item[i].price}</div> <div class="description">${item[i].description}</div> </div>` } } addGoods(arrGoods);创建的排序函数(按价格和名称) function sortByPrise() { var div = document.querySelector("#products"); if (div){ div.innerHTML = ''; this.PriseSort(arrGoods); addGoods(arrGoods); }; } function sortByName() { var div = document.querySelector("#products"); if (div){ div.innerHTML = ''; nameSort(arrGoods); addGoods(arrGoods); }; } function PriseSort(arr){ arr.sort(function(a,b){ return a.price - b.price; }); };如何将这些函数添加到另一个类(例如 Menedger 类)
添加回答
举报
0/150
提交
取消