Differences between MVCarray and Array

I've searched for differences between MVCArray

and Array

for the Google Maps API, but I haven't found specific ones.

It seems to me that this MVCArray

is the best option since it tracks changes (somehow) and has some kind of special events. And some of the same methods have slightly different names, eg. array.length

and MVCarray.getLength()


But can someone explain in a non-professional concept the clear differences between them? and it would be great if you could provide some simple examples to make it easier to understand.



source to share

2 answers

To explain MVCArrays, it is important to understand MVCObjects.

MVCObject is one of the "keys" on which google maps are built. It looks like a simple object with setters and getters for its properties (nothing special so far), but what is special is that:

  • You can add a listener to detect when any property has changed (using the set method) and execute the callback function
  • You can bind any of your properties to another property of another MVCObject (or any class that inherits from this)
  • Other MVCObjects (or any class that inherits from this one) can have their own properties bound to them in the current one.

So basically MVCObjects are bound observables.

An MVCArray inherits from MVCObject so that you can set or get its properties, bind some of them, bind with other objects, etc. But it also implements some additional functions

  • It implements several methods that you can use in your own javascript array, for example push

    , pop

    , forEach

    and so on, so several methods google.maps can take either MVCArray, or its own weight, as it will use the same methods to move them.
  • It has an array property (which can be accessed with a method getArray

    ) which is a native js array. the array-like methods mentioned above are redirected to this array.
  • It implements a set of events so that you can detect when an element in the underlying array is set, removed, or changed.

So basically MVCArrays are observable arrays. And since it inherits MVCObject, their properties can be bound as well



Thanks for your input. From what I read and tried to modify polylines using both array

and MVCarray

to store different coordinates, I found that:

  • So how MVCarray

    has the event (attached to it insert

    , remove

    , set

    ), so when I change any value, the line is immediately updated, and the old line has disappeared.

  • When the value in array

    changes, a new line appears, but the old line is retained.

So it is better to use MVCarray



All Articles