Skip to content

Instantly share code, notes, and snippets.

@thamizhchelvan
Created December 19, 2017 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thamizhchelvan/3cca0d4f3c29410d6d7ec8ae87cf8c67 to your computer and use it in GitHub Desktop.
Save thamizhchelvan/3cca0d4f3c29410d6d7ec8ae87cf8c67 to your computer and use it in GitHub Desktop.
A simple usecase to use call, apply and bind
'use strict';
var trip1 = {
destination: "Chennai",
id: 1
};
var trip2 = {
destination: "Ooty",
id: 2
};
var trip3 = {
destination: "Thanjavur",
id: 3
};
function launchMap(source, travelmode) {
window.open('https://www.google.com/maps/dir/?api=1&origin=' + source + '&destination=' + this.destination + '&travelmode=' + travelmode);
}
launchMap.call(trip1,'Coimbatore', 'driving'); //will launch the map direction immediately between Coimbatore to Chennai
launchMap.apply(trip2,['Coimbatore', 'driving']); //will launch the map direction immediately between Coimbatore to Ooty
//do not launch the map, instead return a function, will call it later once we got the source place.
var callLater = launchMap.bind(trip3);
var userLocation;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(pos => {
if (pos && pos.coords) {
var userLocation = pos.coords.latitude + ',' + pos.coords.longitude;
callLater(userLocation, 'driving');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment