It seems redundant to me that Backbone Collections have both a length property and size function that return the same result. Is there a reason for this?

有帮助吗?

解决方案

One is the size of a particular backbone collection (the length property) the other is a method of the underscore library which can return the size of ANY collection = specifically useful for arrays, lists and anything else it might support.

It's here because all the methods are mixed into a Backbone collection as a convenience thing by the library. It should be noted that this is also a method call -- and not a computed property as well, so it would technically be slower as well to call size instead of just checking the maintained length.

In the interest of sticking with documented API(s) via Backbone itself, you are better off just using length

其他提示

length is a property and will be available only on arrays type, where as size is a method and will be available in most data structure objects like Set, List, etcc..

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top