2

我遇到了一些我得到帮助的 javascript 问题。这是我得到的错误:

Uncaught TypeError: undefined is not a function 

这似乎与我的线路有关:

callback(lastResult); // send the result back

奇怪的是代码似乎按预期工作,但我显然不想让我的代码出错。有人可以帮我看看我哪里出错了吗?

jsfiddle:http: //jsfiddle.net/spadez/uaZkV/3/

完整代码:

$(function () {
    var input = $("#loc"),
        lat = $("#lat"),
        lng = $("#lng"),
        lastQuery = null,
        lastResult = null, // new!
        autocomplete;

    function processLocation(callback) { // accept a callback argument
        var query = $.trim(input.val()),
            geocoder;

        // if query is empty or the same as last time...
        if (!query || query == lastQuery) {
            callback(lastResult); // send the same result as before
            return; // and stop here
        }

        lastQuery = query; // store for next time

        geocoder = new google.maps.Geocoder();
        geocoder.geocode({
            address: query
        }, function (results, status) {
            if (status === google.maps.GeocoderStatus.OK) {
                lat.val(results[0].geometry.location.lat());
                lng.val(results[0].geometry.location.lng());
                lastResult = true; // success!
            } else {
                alert("Sorry - We couldn't find this location. Please try an alternative");
                lastResult = false; // failure!
            }
            callback(lastResult); // send the result back
        });
    }

    autocomplete = new google.maps.places.Autocomplete(input[0], {
        types: ["geocode"],
        componentRestrictions: {
            country: "uk"
        }
    });

    google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

    $('#searchform').on('submit', function (event) {
        var form = this;

        event.preventDefault(); // stop the submission

        processLocation(function (success) {
            if (success) { // if the geocoding succeeded, submit the form
                form.submit()
            }
        });

    });
});
4

2 回答 2

2

文档google.maps.event.addListener暗示,当它调用回调函数 ( )processLocation时,它会在没有任何参数的情况下这样做。

callback是你的函数定义的第一个参数processLocation,所以undefined当谷歌地图代码调用它时。

您尝试将其作为函数调用而不测试它是否为一个函数。

要么删除调用它的尝试,要么typeof先测试它。

于 2013-05-29T16:03:46.843 回答
2
google.maps.event.addListener(autocomplete, 'place_changed', processLocation);

如果您添加processLocation为地图事件侦听器,则在事件触发时将不会传递任何参数。然后callback是参数undefined,并导致您的异常(尽管没有停止您的控制流,因为它是最后一条语句)。

callback这意味着您将在调用之前检查是否传递了实际函数:

function processLocation(callback) { // accept a callback argument
    var query = $.trim(input.val()),
        geocoder;

    // if query is empty or the same as last time...
    if (!query || query == lastQuery) {
        if (typeof callback=="function")
            callback(lastResult); // send the same result as before
        return; // and stop here
    }

    lastQuery = query; // store for next time

    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        address: query
    }, function (results, status) {
        lastResult = status === google.maps.GeocoderStatus.OK;
        if (lastResult) {
            lat.val(results[0].geometry.location.lat());
            lng.val(results[0].geometry.location.lng());
        } else {
            alert("Sorry - We couldn't find this location. Please try an alternative");
        }
        if (typeof callback=="function")
            callback(lastResult); // send the result back
    });
}

如果您有多个调用callback并且不想在任何地方执行类型检查,您还可以定义一个

function noOperation() {}

callback如果没有通过,则将其用作默认值:

function processLocation(callback) { // accept a callback argument
    if (typeof callback != "function")
        callback = jQuery.noop; // predefined by jQuery
    …
}
于 2013-05-29T16:07:26.850 回答