欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对象模拟数组

程序员文章站 2022-03-24 09:37:49
...
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>对象模拟数组</title>
  6. </head>
  7. <body>
  8. <script>
  9. function MyArray() {
  10. this.length = arguments.length;
  11. for(var i=0; i<arguments.length; i++) {
  12. this[i] = arguments[i];
  13. }
  14. this.push = function(s) {
  15. this[this.length]=s;
  16. this.length++;
  17. return this.length;
  18. }
  19. this.pop = function() {
  20. var popdata = this[this.length-1];
  21. delete this[this.length-1];
  22. this.length--;
  23. return popdata;
  24. }
  25. }
  26. var arr = new MyArray(11,3,55,88, 99, "abc");
  27. var arr1 = [2,3434,64,12,56,78]
  28. arr1.forEach(function(value){
  29. console.log(value);
  30. // console.log(index);
  31. // console.log(arr1);
  32. arr.push(value)
  33. })
  34. console.log(arr)
  35. // console.log(typeof arr1);
  36. // console.log(arr1);
  37. </script>
  38. </body>
  39. </html>