博客列表 >对象模拟数组

对象模拟数组

坨坨
坨坨原创
2021年12月06日 22:32:35479浏览
  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>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议