a hard-to-discover problem caused by the empty javascript object

来源:互联网 发布:win10关于windows在哪 编辑:程序博客网 时间:2024/06/14 17:15
In a javascript class, I defined an attribute which is of object type and set an empty object({} or new object()) as the initial value. The problem arose that every item seems to have the exact same value of this attribute. This made me alarmed at the possibility of using the identical empty object created initially. I tested and the result confirmed my hypothesis. The solution is to initialize this attribute later in a lazy fashion. That is to judge whether it is null and initialize it in a method, rather than define directly in the attribute line like java.