Created
December 19, 2017 17:20
-
-
Save thamizhchelvan/3cca0d4f3c29410d6d7ec8ae87cf8c67 to your computer and use it in GitHub Desktop.
A simple usecase to use call, apply and bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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