Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created December 27, 2011 22:37
Show Gist options
  • Save Integralist/1525378 to your computer and use it in GitHub Desktop.
Save Integralist/1525378 to your computer and use it in GitHub Desktop.
Feature Testing a Host Method
/*
* 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