Skip to content

Instantly share code, notes, and snippets.

@efeminella
Created February 29, 2012 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efeminella/1938060 to your computer and use it in GitHub Desktop.
Save efeminella/1938060 to your computer and use it in GitHub Desktop.
Function Overwriting in JavaScript
// Initial "getLocation" implementation. Since we only need to test
// for Geolocation support once, we perform the initial test and then
// overwrite the "getLocation" implementation based on the results of
// the test.
var getLocation = function (success, fail, options)
{
var geolocation = navigator.geolocation;
if ( geolocation )
{
var _options = {
enableHighAccuracy: true,
timeout: 60000,
maximumAge: 0
};
// Geolocation is supported, so we overwrite the implementation
// to simply invoke "geolocation.getCurrentPosition" as there
// is no need to perform the test again.
getLocation = function (success, fail, options)
{
geolocation.getCurrentPosition (success, fail, options || _options);
}
getLocation (success, fail, options);
}
else
{
// Geolocation is not supported, so we overwrite the
// implementation to simply return false (a real
// implementation might provide a polyfill here...).
getLocation = function ()
{
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment