Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试将值推送到数组的属性,如下所示
var obj = {}; obj.a = (obj.a || []).push(10); console.log( typeof obj.a ); // Returning number
如何纠正此问题以将 obj.a 保留为数组?
因为.push [MDN]返回数组的新长度:
.push
返回 调用方法的对象的新长度属性。
你可以做:
(obj.a || (obj.a = [])).push(10);