Guest User

Untitled

a guest
May 27th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. // attempt 1
  2. var a = {};
  3. a["foo"] = "bar";
  4.  
  5. function get(hash, key) {
  6. return(hash[key]);
  7. }
  8.  
  9. if (get(a, k)) alert("bingo"); // alerts when k is "constructor"
  10.  
  11.  
  12. // attempt 2
  13. var a = {};
  14. a["foo"] = "bar";
  15.  
  16. function get(hash, key) {
  17. if (hash.hasOwnProperty(key)) return(hash[key]);
  18. }
  19.  
  20. if (get(a, "constructor")) alert("bingo"); // looks good now
  21.  
  22.  
  23. // attempt 3
  24. var a = {};
  25. a["foo"] = "bar";
  26. a["hasOwnProperty"] = 0; // oops! hides the built-in method
  27.  
  28. function get(hash, key) {
  29. if (hash.hasOwnProperty(key)) return(hash[key]); // throws fatal error
  30. }
  31.  
  32. if (get(a, "constructor")) alert("bingo");
Add Comment
Please, Sign In to add comment