1、使用匿名函数
var donuts = [
{ type: "Jelly", cost: 1.22 },
{ type: "Chocolate", cost: 2.45 },
{ type: "Cider", cost: 1.59 },
{ type: "Boston Cream", cost: 5.99 }
];
donuts.forEach(function(e){
console.log(e.type+" donuts "+"cost $"+e.cost+" each");
});
2、不使用匿名函数
var donuts = [
{ type: "Jelly", cost: 1.22 },
{ type: "Chocolate", cost: 2.45 },
{ type: "Cider", cost: 1.59 },
{ type: "Boston Cream", cost: 5.99 }
];
function f(e)
{
console.log(e.type+" donuts "+"cost $"+e.cost+" each");
}
donuts.forEach(f,donuts);