Created
December 27, 2011 22:37
-
-
Save Integralist/1525378 to your computer and use it in GitHub Desktop.
Feature Testing a Host Method
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
/* | |
* Feature Testing a Host Method | |
* Because a callable host object can legitimately have any typeof result then it can't be relied upon. | |
* | |
* @notes: | |
* The reason for the && !!object[property] is because in ECMAScript version 3, | |
* a null object has typeof result 'object' (which is considered a bug). | |
* In future versions (ECMAScript 5+) the typeof result will be 'null' (as it should be). | |
* | |
* @reference: http://michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting | |
*/ | |
function isHostMethod(object, property) { | |
var type = typeof object[property]; | |
return type == 'function' || // For Safari 3 typeof result being 'function' instead of 'object' | |
(type == 'object' && !!object[property]) || // Protect against ES3 'null' typeof result being 'object' | |
type == 'unknown' || // For IE < 9 when Microsoft used ActiveX objects for Native Functions | |
type == 'string'; // typeof for 'document.body[outerHTML]' results in 'string' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment