javascript-collections-framework
    Preparing search index...

    Function unmodifiableCollection

    • Returns a read-only view of collection: reads, iteration and cursors go to the collection, so the owner's changes show through, and every method that would change it throws UnsupportedOperationError. The view compares by identity (equals is true only for itself), because a collection of any family can be wrapped.

      When collection is already a view this function returns (printed as UnmodifiableCollection), it is returned unchanged. When it is any other unmodifiable view or immutable collection from this copy of the library, the new view reads that collection's source instead, and entries it hands out stay read-only, so read-only wrappers never nest.

      Time: O(1); each operation costs what the collection's costs.

      Type Parameters

      • E

      Parameters

      Returns ReadonlyCollection<E>

      collection itself, or an unmodifiable view printed as UnmodifiableCollection.

      TypeError when collection is not a collection from this library.

      const queue = new ArrayDeque(['a'])
      const view = unmodifiableCollection(queue)
      queue.push('b')
      view.size // 2