Adding properties to Object.prototype breaks Google Maps

353 views
Skip to first unread message

Nick

unread,
Aug 2, 2010, 11:46:08 PM8/2/10
to Google Maps JavaScript API v3
I notice that when any property is added to Object.prototype (a
variable or a function), Google Maps will fail to load the map.

Here is an example demonstrating the problem: http://dl.dropbox.com/u/9699560/map.html

If you comment the line that says 'Object.prototype.pi = 3.1415;', the
map will load.

Web developer gives me this error:

Error: Invalid value: [object Object] (Error in property <pi>: (a[d]
is not a function))
Source File: http://maps.gstatic.com/intl/en_us/mapfiles/api-3/1/8a/main.js
Line: 16

And Firebug says:

a[d] is not a function

I'm not sure what causes this, and I can't even begin to debug the
minified Google code, so I was wondering if anyone had a fix or a
workaround.

Having access to Object.prototype is important, because I implement
Douglas Crockford's Object.prototype.beget method (which will be added
to the next ECMAScript version as Object.create).

Thank you for the help!

William

unread,
Aug 3, 2010, 2:02:49 AM8/3/10
to Google Maps JavaScript API v3
On Aug 3, 1:46 pm, Nick <gummyb...@gmail.com> wrote:
> Having access to Object.prototype is important, because I implement
> Douglas Crockford's Object.prototype.beget method (which will be added
> to the next ECMAScript version as Object.create).
>

the Maps API fully utilizes functional programming techniques,
especially the map() function which applies a function to all elements
of a list, or all properties of an object. But these iterators can
break when inheriting unexpected properties from Object.

In this case it seems to be related to precondition assertions, that
the values of a StreetViewPov heading, pitch and zoom are all
numbers. There is a constructor for StreetViewPov, and also another
object which has the assertion function corresponding to each
property. But pi is unexpected, and there is no function pi() to
check whether it has a valid value:

var obj = {
zoom: L,
heading: L,
pitch: L
}

where L() is a function which verifies something is a number.

A workaround would be to use Crockford's first technique, a global
function called object(prototype), see the following link:
http://javascript.crockford.com/prototypal.html

...

Ben Appleton

unread,
Aug 3, 2010, 2:20:51 AM8/3/10
to google-map...@googlegroups.com
Hi Nick,

Please do not add new properties to Object.prototype. It is for good
reason that Crockford's .beget method is not going to be added to
Object.prototype - that could break many existing codebases much as
you've found with Maps API v3. Working around unexpected properties
on Object.prototype will slow down the various places we use Objects
as hash maps, as William points out.

I suggest using Object.beget: http://www.room51.co.uk/js/beget.html.

Cheers
Ben

> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> To post to this group, send email to google-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
>

Nick

unread,
Aug 3, 2010, 6:24:10 AM8/3/10
to Google Maps JavaScript API v3
Alright, thanks for your all's help! I will find a different means of
inheritance in my code.

On Aug 3, 2:20 am, Ben Appleton <apple...@google.com> wrote:
> Hi Nick,
>
> Please do not add new properties to Object.prototype.  It is for good
> reason that Crockford's .beget method is not going to be added to
> Object.prototype - that could break many existing codebases much as
> you've found with Maps API v3.  Working around unexpected properties
> on Object.prototype will slow down the various places we use Objects
> as hash maps, as William points out.
>
> I suggest using Object.beget:http://www.room51.co.uk/js/beget.html.
>
> Cheers
> Ben
>

William

unread,
Aug 3, 2010, 5:58:40 PM8/3/10
to Google Maps JavaScript API v3
On Aug 3, 8:24 pm, Nick <gummyb...@gmail.com> wrote:
> I will find a different means of inheritance in my code.

here's an article comparing inheritance in the Closure library with
the techniques from Crockford's book:
http://bolinfest.com/javascript/inheritance.php

The V3 MarkerClusterer has an implementation of extend() which is
quite interesting:

function MarkerClusterer(map, opt_markers, opt_options) {
this.extend(MarkerClusterer, google.maps.OverlayView);
...
}

MarkerClusterer.prototype.extend = function(obj1, obj2) {
return (function(object) {
for (property in object.prototype) {
this.prototype[property] = object.prototype[property];
}
return this;
}).apply(obj1, [obj2]);
};

...
Reply all
Reply to author
Forward
0 new messages