【发布时间】:2016-09-21 06:28:58
【问题描述】:
想知道避免使用 var me = this; 的最佳方法在以下上下文中。此方法存在于 React 组件内部。我过去使用过 underscore.js 方法 _.bind() - 有没有更反应的方法?
findRoutes: function(){
var me = this;
if (!this.state.originId || !this.state.destinationId) {
alert("findRoutes!");
return;
}
var p1 = new Promise(function(resolve, reject) {
directionsService.route({
origin: {'placeId': me.state.originId},
destination: {'placeId': me.state.destinationId},
travelMode: me.state.travelMode
}, function(response, status){
if (status === google.maps.DirectionsStatus.OK) {
// me.response = response;
directionsDisplay.setDirections(response);
resolve(response);
} else {
window.alert('Directions config failed due to ' + status);
}
});
});
return p1
},
【问题讨论】:
标签: javascript reactjs binding this