var t1 = [];
var t2 = [];
var arr = [
{
id: 1,
name: "a1"
},
{
id: 2,
name: "a2"
},
{
id: 3,
name: "a3"
}
]
function test(id, name) {
this.id = id
this.name = name
this.idAndName = id + name
}
arr.forEach((item, index) => {
// 1
t1.push({
id: item.id,
name: item.name,
idAndName: item.id + item.name
})
// 2new
t2.push(new test(item.id, item.name));
})
console.log(t1, t2);
Which of the above two ways of writing is better? What"s the difference