search

Home  >  Q&A  >  body text

请问javascript 中怎么给回调函数传值?

例如我要使用geolocation的api,里面有getcurrentlocation方法,如何将外部定义的map传给successCallback内部?

var map = new googleMap
navigator.geolocation.watchPosition(successCallback,errorCallback,options);

function succesCallback(position){//这里position是默认的,我需要将外部定义的map引入,如果我加了新的传入参数会报错。
    map.latitude = position.coords.latitude
    map.longitude =  position.coords.longitude
}

期待解答

黄舟黄舟2902 days ago283

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-04-10 15:13:36

    var app = {
        getGeoLoc : function (id) {
            var self = this;
            navigator.geolocation.getCurrentPosition(function(position) {
                var myVar1, myVar2, myVar3;
                self.foundLoc(position, self, myVar1, myVar2, myVar3)
            }, this.noloc, { timeout : 3});
        },
        foundLoc : function(position, self, myVar1, myVar2, myVar3) {},
    };
    

    reply
    0
  • 阿神

    阿神2017-04-10 15:13:36

    亲,你这样写的代码就可以完成你的要求(不谈这样写好不好的问题)

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-10 15:13:36

    var map = new googleMap
    

    map 是在全局作用域,回调函数 succesCallback 能访问 map 的。

    reply
    0
  • 黄舟

    黄舟2017-04-10 15:13:36

    sucessCallback.bind(null, /* 你想传的值 */map);

    reply
    0
  • Cancelreply