Skip to content

Instantly share code, notes, and snippets.

@dciccale
Last active December 11, 2015 08:18
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 dciccale/4572294 to your computer and use it in GitHub Desktop.
Save dciccale/4572294 to your computer and use it in GitHub Desktop.
hasOwnProperty shim in 115 bytes (counting only the actual function)

hasOwnProperty shim

Cross-browser hasOwnProperty shim in 115 bytes of JavaScript.

<!doctype html>
<title>Demo</title>
<script>
window.hop = function(o,p,u){u='undefined';return typeof o==u||typeof p==u||typeof o[p]==u?!1:o[p]!==o.constructor.prototype[p]};
var obj = {
name: 'Denis'
};
alert(hop(obj, 'name'); // true
alert(!!obj.toString); // true
alert(hop(obj, 'toString'); // false
</script>
window.hop = function (obj, prop) {
if (typeof obj == 'undefined' || typeof prop == 'undefined' || typeof obj[prop] == 'undefined') {
return false;
} else {
return obj[prop] !== obj.constructor.prototype[prop];
}
}
window.hop = function(o,p,u){u='undefined';return typeof o==u||typeof p==u||typeof o[p]==u?!1:o[p]!==o.constructor.prototype[p]};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment