has.js 986 B

12345678910111213141516171819202122232425262728293031323334
  1. var inspect = require('../');
  2. var test = require('tape');
  3. function withoutProperty(object, property, fn) {
  4. var original;
  5. if (Object.getOwnPropertyDescriptor) {
  6. original = Object.getOwnPropertyDescriptor(object, property);
  7. } else {
  8. original = object[property];
  9. }
  10. delete object[property];
  11. try {
  12. fn();
  13. } finally {
  14. if (Object.getOwnPropertyDescriptor) {
  15. Object.defineProperty(object, property, original);
  16. } else {
  17. object[property] = original;
  18. }
  19. }
  20. }
  21. test('when Object#hasOwnProperty is deleted', function (t) {
  22. t.plan(1);
  23. var arr = [1, , 3]; // eslint-disable-line no-sparse-arrays
  24. // eslint-disable-next-line no-extend-native
  25. Array.prototype[1] = 2; // this is needed to account for "in" vs "hasOwnProperty"
  26. withoutProperty(Object.prototype, 'hasOwnProperty', function () {
  27. t.equal(inspect(arr), '[ 1, , 3 ]');
  28. });
  29. delete Array.prototype[1];
  30. });