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