I have been looking up the differences between MVCArray
and Array
for Google map API, but I haven't found any concrete.
I kind of get that MVCArray
is a better option since it keeps track of changes (somehow) and have some kind of special events. And some of the same methods have slightly different names, e.g. array.length
and MVCarray.getLength()
.
But can someone please explain in layman's term the explicit differences between the two? and it would be great if you could give some simple examples to make it easier to understand.
Thank you!
I have been looking up the differences between MVCArray
and Array
for Google map API, but I haven't found any concrete.
I kind of get that MVCArray
is a better option since it keeps track of changes (somehow) and have some kind of special events. And some of the same methods have slightly different names, e.g. array.length
and MVCarray.getLength()
.
But can someone please explain in layman's term the explicit differences between the two? and it would be great if you could give some simple examples to make it easier to understand.
Thank you!
Share Improve this question edited Mar 28, 2017 at 8:59 Peter Tretiakov 3,4106 gold badges39 silver badges56 bronze badges asked Mar 28, 2017 at 8:20 lazy bumlazy bum 791 silver badge4 bronze badges 1- This is probably the best / only explanation I've found. – duncan Commented Mar 28, 2017 at 8:27
2 Answers
Reset to default 8To explain MVCArrays it's important to understand MVCObjects.
MVCObject is one of the "keystone" classes google maps is built on. It's like a plain object with setters and getters for its properties (nothing special so far), but what makes it special is that:
- You can add a listener to detect when any property is changed (with the set method) and execute a callback function
- You can bind any of its properties to another property of other MVCObject (or any class that inherits from this one)
- Other MVCObjects (or any class that inherit from this one) can have its properties binded to the ones in the current one.
So, basically, MVCObjects are observable-bindable objetcs.
An MVCArray inherits from MVCObject so you can set or get its properties, bind some of them, be binded by other objects, etc. But also, it implements some extras
- It implements several of the methods you could use in a native javascript array, like
push
,pop
,forEach
, etc, so several google.maps methods can accept either an MVCArray or a native array, since it will use the same methods to traverse them. - It has an array property (that you can access with
getArray
method) that is a native js array. array-like methods as mentioned above are forwarded to this array. - It implements a set of events so 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 from MVCObject, their properties are also bindable
Thank you for your input. From what I have read and tried out changing polylines using both array
and MVCarray
to store different coordinates, I found that:
Since
MVCarray
has events attached to it (insert
,remove
,set
) so when I change any value, the line is updated immediately and the old line is gone.When value in
array
is changed, the new line also appears but the
old line is retained.
Thus, it's better to use MVCarray