Home  >  Article  >  Web Front-end  >  Share an example tutorial rewritten into a traditional callback function

Share an example tutorial rewritten into a traditional callback function

零下一度
零下一度Original
2017-06-26 11:50:061193browse

Before rewriting:

##JavaScript API example of coordinate conversion in Baidu MapThe official example is as follows:

var points = [new BMap.Point(116.3786889372559,39.90762965106183),              new BMap.Point(116.38632786853032,39.90795884517671),              new BMap.Point(116.39534009082035,39.907432133833574),              new BMap.Point(116.40624058825688,39.90789300648029),              new BMap.Point(116.41413701159672,39.90795884517671)
];//地图初始化var bm = new BMap.Map("allmap");
bm.centerAndZoom(new BMap.Point(116.378688937,39.9076296510), 15);//坐标转换完之后的回调函数translateCallback = function (data){  if(data.status === 0) {for (var i = 0; i < data.points.length; i++) {
        bm.addOverlay(new BMap.Marker(data.points[i]));
        bm.setCenter(data.points[i]);
    }
  }
}
setTimeout(function(){var convertor = new BMap.Convertor();
    convertor.translate(points, 1, 5, translateCallback)
}, 1000);
After rewriting:

Rewrite the above code into the traditional JavaScript callback function form:

function translate(points,callback){  var convertor=new BMap.Convertor();
  convertor.translate(points, 1, 5, callback)
  translateCallback = function (data){if(data.status === 0) {      var res=[]; //res用来存放转换后的points数组  for (var i = 0; i < data.points.length; i++) {
          bm.addOverlay(new BMap.Marker(data.points[i]));
          res.push(data.points[i]);
      }
      callback(res)
    }
  }
}//再调用translate函数,translate(points,function(data){
    console.log(data); //data为转换后的points数组})
After rewriting, the translate function accepts two parameters. The first parameter is the original Baidu points array, and the second parameter is the original Baidu points array. Parameters are passed into a callback function.

The above is the detailed content of Share an example tutorial rewritten into a traditional callback function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Share a js example I madeNext article:Share a js example I made