Blogger Information
Blog 7
fans 0
comment 0
visits 3288
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript对象模拟数组
凝思
Original
437 people have browsed it

JavaScript对象模拟数组

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>JS对象模拟数组</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. this.toString = function (){
  26. var result = "";
  27. var j = ',';
  28. for (var i = 0; i<this.length-1; i++){
  29. result += this[i];
  30. result += j;
  31. }
  32. result += this[i];
  33. return result;
  34. }
  35. this.sort = function sort(arr, flag=true) {
  36. for(var i=0; i<arr.length-1; i++) {
  37. for (var j = 0; j < arr.length-i-1; j++) {
  38. if(flag) {
  39. var swap = arr[j]
  40. if (arr[j] > arr[j + 1]) {
  41. arr[j] = arr[j + 1];
  42. arr[j + 1] = swap;
  43. }
  44. }else{
  45. if (arr[j] < arr[j + 1]) {
  46. arr[j] = arr[j + 1];
  47. arr[j + 1] = swap;
  48. }
  49. }
  50. }
  51. }
  52. return arr;
  53. }
  54. this.max = function arrmax(arr) {
  55. var max = arr[0];
  56. for(var i=0; i<arr.length; i++) {
  57. if(arr[i]>max)
  58. max=arr[i];
  59. }
  60. return max;
  61. }
  62. this.min = function arrmin(arr) {
  63. var min = arr[0];
  64. for(var i=0; i<arr.length; i++) {
  65. if(arr[i]<min)
  66. min=arr[i];
  67. }
  68. return min;
  69. }
  70. this.reverse = function() {
  71. var result = [];
  72. for(var i = 0; i < this.length; i++) {
  73. result[result.length] = this[this.length - i - 1];
  74. }
  75. for(var i = 0; i < result.length; i++) {
  76. this[i] = result[i];
  77. }
  78. return this;
  79. }
  80. }
  81. var arr = new MyArray(1,4,9,3,7)
  82. console.log(arr.push("hello"))
  83. console.log(arr.push("world"))
  84. console.log(arr.toString())
  85. console.log(arr.pop())
  86. console.log(arr.pop())
  87. console.log(arr.sort(arr,true))
  88. console.log(arr.max(arr))
  89. console.log(arr.min(arr))
  90. console.log(arr.reverse())
  91. </script>
  92. </body>
  93. </html>

效果图

对象模拟数组

小知识点

数组对象的push与pop方法分别在数组的尾部添加与删除元素。
sort() 方法用于对数组的元素进行排序。
max()、min()取数组参数中的最大值、最小值

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post