Guest User

Untitled

a guest
Jan 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. Object.getOwnPropertyNames = Object.getOwnPropertyNames || function (object) {
  2. var props = [];
  3. for (var i in object) {
  4. if (object.hasOwnProperty(i)) {
  5. props.push(i);
  6. }
  7. }
  8. return props;
  9. };
  10.  
  11. Object.keys = Object.keys || function (object) {
  12. var keys = [];
  13. for (var i in object) {
  14. keys.push(i);
  15. }
  16. return keys;
  17. };
  18.  
  19. Object.getPropsAndValues = function (object) {
  20. var result = '';
  21. for (var i in obj) {
  22. if (object.hasOwnProperty(i)) {
  23. result += i + ': ' + '\n';
  24. }
  25. }
  26. // strip unneccessary newline at end
  27. result = result.replace(/\n$/);
  28. };
  29.  
  30. // Console
  31.  
  32. >>> var obj = {a: 1, b: 2};
  33. >>> Object.prototype.c = 3;
  34. >>>
  35. >>> Object.getOwnPropertyNames(obj);
  36. ["a", "b"]
  37. >>> Object.keys(obj);
  38. ["a", "b", "c"]
  39. >>> Object.getPropsAndValues(obj);
  40. "a: 1
  41. b: 2"
Add Comment
Please, Sign In to add comment