📜  javascript中的function和for是如何工作的? - Javascript代码示例

📅  最后修改于: 2022-03-11 15:03:46.507000             🧑  作者: Mango

代码示例1
const products = [
    { name: 'Laptop', price: 32000, brand: 'Lenovo', color: 'Silver' },
    { name: 'Phone', price: 700, brand: 'Iphone', color: 'Golden' },
    { name: 'Watch', price: 3000, brand: 'Casio', color: 'Yellow' },
    { name: 'Aunglass', price: 300, brand: 'Ribon', color: 'Blue' },
    { name: 'Camera', price: 9000, brand: 'Lenovo', color: 'Gray' },
];
function myFun() {
    let getTotal = 0;
    for (let product of products) { //for of loop
        getTotal += product.price; //getTotal = getTotal + product.price;
    }
    return getTotal;
}
const result = myFun();
console.log(result);
//Expected output:45000