Created
February 29, 2012 05:13
-
-
Save efeminella/1938060 to your computer and use it in GitHub Desktop.
Function Overwriting in JavaScript
This file contains hidden or 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
// 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