push1.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 write(v) { WScript.Echo(v); }
  6. var n = 5;
  7. function InitObject(obj) {
  8. for (var i=0; i<n; i++) {
  9. obj[i] = i * i + 1;
  10. }
  11. obj.length = n;
  12. return obj;
  13. }
  14. function TestPush(obj) {
  15. write(">>> Start push test for object: " + obj);
  16. var ret;
  17. ret = Array.prototype.push.call(obj);
  18. write("Returned:" + ret + " obj.length:" + obj.length);
  19. ret = Array.prototype.push.call(obj, "");
  20. write("Returned:" + ret + " obj.length:" + obj.length);
  21. ret = Array.prototype.push.call(obj, undefined);
  22. write("Returned:" + ret + " obj.length:" + obj.length);
  23. ret = Array.prototype.push.call(obj, 100);
  24. write("Returned:" + ret + " obj.length:" + obj.length);
  25. ret = Array.prototype.push.call(obj, 1, 2);
  26. write("Returned:" + ret + " obj.length:" + obj.length);
  27. ret = Array.prototype.push.call(obj, 1, 2, 3, 4, 5);
  28. write("Returned:" + ret + " obj.length:" + obj.length);
  29. write("<<< Stop push test for object: " + obj);
  30. }
  31. var testList = new Array(new Array() , new Object());
  32. for (var i=0;i<testList.length;i++) {
  33. TestPush(InitObject(testList[i]));
  34. }
  35. TestPush({}); // behavior varies by version
  36. function bar()
  37. {
  38. var n = Number();
  39. Number.prototype.push = Array.prototype.push;
  40. n.push(1);
  41. }
  42. bar();