views:

94

answers:

4

I'm using a plugin for jQuery. It works great in webkit, but when I try it in firefox I get the following firefox error:

google.maps.Geocoder is not a constructor
$('.to, .from').geo_autocomplete(new google.maps.Geocoder, { 

Here is all the jquery:

$('.to, .from').geo_autocomplete(new google.maps.Geocoder, {
mapkey: 'ABQIAAAAbnvDoAoYOSW2iqoXiGTpYBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQNumU68AwGqjbSNF9YO8NokKst8w', 
selectFirst: false,
minChars: 3,
cacheLength: 50,
width: 235,
scroll: true,
scrollHeight: 330
});

What's a constructor and howcome firefox is pointing it out to me?

http://dev.resihop.nu is the site

+6  A: 

A constructor is the function (which returns an object of the function name's type) that is invoked when you use new in conjunction with that function's name, such as:

function Person(name, age) {
   //blah
}

var me = new Person("Jacob", 20);
Jacob Relkin
`new` will cause the given function to be called as a constructor even if you omit the trailing `()`; this is one of JavaScript's odd syntax quirks. The problem here is more basic than lack of parentheses.
bobince
This isn't passing a reference. It is calling the constructor with the `new` keyword and passing zero arguments. Parentheses are not required for constructors that don't require arguments.
patrick dw
+1  A: 

You msut use google.maps.geocoder like this:

$('.to, .from').geo_autocomplete(new google.maps.Geocoder({
  mapkey:'ABQIAAAAbnvDoAoYOSW2iqoXiGTpYBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQNumU68AwGqjbSNF9YO8NokKst8w', 
  selectFirst: false,
  minChars: 3,
  cacheLength: 50,
  width: 235,
  scroll: true,
  scrollHeight: 330
}));
madeinstefano
+1  A: 

When you instantiate an object, e.g. create an instance of an object, the constructor is the first method that is called within your object.

When you're calling

new google.maps.Geocoder

...you are attempting instantiate a parameterless constructor of the object by using the new keyword. In this case, Geocoder is not a class that can be instantiated without parameters, or at all.

George
+4  A: 

Any native function may be called as a constructor (even if it wasn't designed to be). Anything that's not callable also cannot be a constructor. eg new 3 gives the same error.

In your page, google.maps.Geocoder is simply undefined, which certainly isn't going to help. Looking at Google's maps script it's failing to load the Geocoder module because it's using document.write to do so, a method that has to be run from a <script> included in the HTML document at parse time, not imported by use of DOM scripting as you're doing here.

It certainly doesn't expect to be run from a page loaded via client-side XSLT. This is going to give you lots of browser problems and zero SEO presence. What is the purpose of this craziness?

bobince
Strictly speaking, the spec says that `new` invokes the `[[Construct]]` internal method, while a plain function call uses `[[Call]]`. Now that said I have no idea what sort of thing would have one but not the other, but perhaps some things exposed via the global namespace might - what does `new setTimeout()` do?
Pointy
Yeah, that's why I said *native* function. :-) Actually I think per ECMA262-5, *built-in* functions that aren't meant to be used as constructors shouldn't have a `[[Construct]]`, and so should `TypeError`. What actually happens is highly browser- and function-dependent. Opera seems to get it right. Chrome calls `new encodeURIComponent('x')` as a constructor (returning the new Object as the returned string is not an object). Firefox varies (eg `new setTimeout` gives a different internal error; `new encodeURIComponent('x')` returns the string!). In summary: argh!
bobince
Thanks for the info, although I'm not sure how to work it out yet, but you got me started atleast!
Kristoffer Nolgren
asfor the frontend xslt, it's really not my desciscion, I'd prefer to run it backend. However I have had no other browserproblems with it, our plan is to push backend-generated html to any robot or other none-identifiable reader of the page.
Kristoffer Nolgren