reflectKeysTest.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function test1() {
  6. var sym1 = Symbol(), sym2 = Symbol(), sym3 = Symbol();
  7. var obj = {
  8. 1: true,
  9. A: true,
  10. };
  11. obj.B = true;
  12. obj[sym1] = true;
  13. obj[2] = true;
  14. obj[sym2] = true;
  15. Object.defineProperty(obj, 'C', { value: true, enumerable: true });
  16. Object.defineProperty(obj, sym3, { value: true, enumerable: true });
  17. Object.defineProperty(obj, 'D', { value: true, enumerable: true });
  18. // Reflect.ownKeys
  19. print('Reflect.ownKeys');
  20. var result = Reflect.ownKeys(obj);
  21. for (var i in result) {
  22. print(result[i].toString());
  23. }
  24. // Object.getOwnPropertySymbols
  25. print('Object.getOwnPropertySymbols');
  26. result = Object.getOwnPropertySymbols(obj);
  27. for(var i in result) {
  28. print(result[i].toString());
  29. }
  30. }
  31. function test2() {
  32. function test() { };
  33. Object.defineProperty(test, 'A', { value: true, enumerable: true });
  34. Object.defineProperty(test, Symbol('blah'), { value: true, enumerable: true });
  35. Object.defineProperty(test, 'D', { value: true, enumerable: true });
  36. // special properties
  37. print('Reflect.ownKeys with special properties');
  38. result = Reflect.ownKeys(test);
  39. for (var i in result) {
  40. print(result[i].toString());
  41. }
  42. }
  43. function test3() {
  44. var x = {};
  45. Object.defineProperty(x, "a", { value: 5, enumerable: true });
  46. Object.defineProperty(x, "b", { get: function () { return 23; }, enumerable: true });
  47. var p = new Proxy(x, {
  48. getOwnPropertyDescriptor: function (target, property) {
  49. return Reflect.getOwnPropertyDescriptor(target, property);
  50. }
  51. });
  52. print(Object.keys(p));
  53. }
  54. function test4() {
  55. function bar() { };
  56. var foo = new Proxy(bar, {});
  57. print(Object.getOwnPropertyNames(foo));
  58. print(Reflect.ownKeys(foo));
  59. }
  60. test1();
  61. test2();
  62. test3();
  63. test4();