How to quickly clear a JavaScript Object?

后端 未结 8 1138
一个人的身影
一个人的身影 2020-11-27 12:52

With a JavaScript Array, I can reset it to an empty state with a single assignment:

array.length = 0;

This makes the Array \"appear\" empty

8条回答
  •  长情又很酷
    2020-11-27 13:05

    Something new to think about looking forward to Object.observe in ES7 and with data-binding in general. Consider:

    var foo={
       name: "hello"
    };
    
    Object.observe(foo, function(){alert('modified');}); // bind to foo
    
    foo={}; // You are no longer bound to foo but to an orphaned version of it
    foo.name="there"; // This change will be missed by Object.observe()
    

    So under that circumstance #2 can be the best choice.

提交回复
热议问题