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 }) // 写法2,通过new的方式添加 t2.push(new test(item.id, item.name)); })console.log(t1, t2);
上面的两种写法哪种方式好些?有什么区别
qq_花开花谢_0
杨魅力
相关分类