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.
每次从服务器获取数据时,我都想丢弃 observablearray 中的数据。
功能上有什么区别
self.myArray([]);
对比
self.myArray.removeAll();
从最终结果的角度来看,这两个调用没有区别,因此您最终将不myArray包含任何元素。
myArray
但是有一个小区别(如果您不关心不同的返回值):
将用新创建的空数组替换底层数组实例。
虽然
将从底层数组中删除所有项目,但会保留数组实例。
因此,如果您有多个ko.observableArray使用相同的底层数组,您可以看到两个调用之间的区别:
ko.observableArray
演示JSFiddle。