es6HasInstance.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // normal function
  6. function test1() {
  7. print('*** test1');
  8. var o = function (a, b) {
  9. this.x = a;
  10. this.y = b;
  11. }
  12. Object.defineProperty(o, Symbol.hasInstance, {
  13. value: function () {
  14. print('checked Symbol.hasInstance');
  15. return true;
  16. }
  17. });
  18. print(({}) instanceof o);
  19. }
  20. // bounded function
  21. function test2() {
  22. print('*** test2');
  23. var o = function (a, b) {
  24. this.x = a;
  25. this.y = b;
  26. }
  27. var bounded = o.bind(1, 2);
  28. Object.defineProperty(o, Symbol.hasInstance, {
  29. value: function () {
  30. print('checked Symbol.hasInstance');
  31. return true;
  32. }
  33. });
  34. var x = Object.create(bounded);
  35. print((x) instanceof o);
  36. print((bounded) instanceof o);
  37. }
  38. // proxy of function
  39. function test3() {
  40. print('*** test3');
  41. function foo() { };
  42. Object.defineProperty(foo, Symbol.hasInstance, {
  43. value: function () {
  44. print('checked Symbol.hasInstance');
  45. return false;
  46. }
  47. });
  48. var proxy = new Proxy(foo, {
  49. get : function (target, property){
  50. print(property.toString());
  51. return Reflect.get(target, property);
  52. }
  53. });
  54. var x = new proxy();
  55. print((x) instanceof proxy);
  56. }
  57. // proxy of bounded function
  58. function test4() {
  59. print('*** test4');
  60. function foo() { };
  61. Object.defineProperty(foo, Symbol.hasInstance, {
  62. value: function () {
  63. print('checked Symbol.hasInstance');
  64. return false;
  65. }
  66. });
  67. var proxy = new Proxy(foo, {
  68. get: function (target, property) {
  69. print(property.toString());
  70. return Reflect.get(target, property);
  71. }
  72. });
  73. var x = proxy.bind();
  74. print((x) instanceof proxy);
  75. }
  76. test1();
  77. test2();
  78. test3();
  79. test4();