Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. Object.prototype.hasProperty = function(properties) {
  2. var finalProperty = this;
  3.  
  4. if (properties.constructor !== Array) {
  5. properties = [properties];
  6. }
  7.  
  8. try {
  9. properties.forEach(function checkProperty(property) {
  10. finalProperty = finalProperty[property];
  11. });
  12. } catch (e) {
  13. return false;
  14. }
  15.  
  16. return finalProperty !== undefined;
  17. }
  18.  
  19. var a = {b: {c: {}}};
  20.  
  21. if (a.hasProperty(['b', 'c'])) {
  22. console.log("definido!");
  23. } else {
  24. console.log("indefinido!");
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement