⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wm_array_ehm.html

📁 有很多很实用的方法
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    <form>
      <p><font face="宋体"><textarea cols="45" rows="20" wrap="off" name="wm_array_ehm">
&lt;script language=&quot;JavaScript&quot;&gt;
<!--



/*
Webmonkey Array Enhancements

JavaScript 1.2, which only works in Netscape 
4.0, adds several fundamental methods to the 
array object. A few of these very useful 
methods are included here in a form that 
can be used by 3.0 browsers.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Thau!
Author Email: thau@wired.com
*/

// copy
//
// Copies the contents of an array into a temp 
// array and returns the copy.
//
// It doesn't alter the original array, but be 
// careful if the array contains references to 
// objects instead of just strings or numbers. 
// References to objects will be copied as 
// references. This means that if you change 
// an object in the original array, it will 
// also be changed in the new array.
//
function copy()
{
  var loop;
  var temp_array = new Array();
  for (loop = 0; loop < this.length; loop++)
  {
    temp_array[loop] = this[loop];
  }
  return temp_array;
}

Array.prototype.copy = copy;

// pop
//
// Removes the last element of an array and 
// returns that element. 
//
function pop()
{
  var last_item = this[this.length-1];
  this.length--;
  return last_item;
}

Array.prototype.pop = pop;

// push
//
// Adds an element to the end of an array and 
// returns the new length of the array. This 
// method changes the length of the array. 
//
function push(the_item)
{
  this[this.length] = the_item;
  return this.length;
}

Array.prototype.push = push;

// concat
//
// Joins two arrays and returns a new array. 
// It doesn't alter the original arrays, but be 
// careful if the arrays contain references to 
// objects instead of just strings or numbers. 
// References to objects will be copied as 
// references. This means that if you change 
// an object in the original arrays, it will 
// also be changed in the new array.
//
function concat(second_array)
{
  var first_array = this.copy();
  
  for (loop = 0; loop<second_array.length; loop++)
  {
    first_array[first_array.length] = second_array[loop];
  }
  return first_array;
}

Array.prototype.concat = concat;

// shift
//
// Removes the first element of an array and 
// returns that element.
//
function shift()
{
  var new_value = this[0];
  var orig_length = this.length;
  for (loop = 0; loop<this.length-1; loop++)
  {
    this[loop] = this[loop+1];
  }
  this.length--;
  return new_value;
}

Array.prototype.shift = shift;

// unshift
//
// Adds an element to the beginning of an array and 
// returns the new length of the array.
//
function unshift(the_item)
{
  for (loop = this.length-1 ; loop >= 0; loop--)
  {
    this[loop+1] = this[loop];
  }
  this[0] = the_item;
  return this.length;
}

Array.prototype.unshift = unshift;

// permute
//
// Randomly reorders the elements of an 
// array and returns the new, reordered array. 
// The original array remains unchanged.
// 
function permute(the_array)
{

  var temp_array = this.copy();
  var new_array = new Array();
  var random_num = 0;
  for (loop = 0; loop < this.length; loop++)
  {
    random_num = Math.round(Math.random() * (temp_array.length-1));
    new_array[loop] = temp_array[random_num];
    temp_array[random_num] = temp_array[temp_array.length-1];
    temp_array.length--;
  }
  return new_array;
}

Array.prototype.permute = permute;


// -->

&lt;/script&gt;

</textarea><this.length; loop++) { temp_array[loop]="this[loop];" } return temp_array; } Array.prototype.copy="copy;" // pop // // Removes the last element of an array and // returns that element. // function pop() { var last_item="this[this.length-1];" this.length--; return last_item; } Array.prototype.pop="pop;" // push // // Adds an element to the end of an array and // returns the new length of the array. This // method changes the length of the array. // function push(the_item) { this[this.length]="the_item;" return this.length; } Array.prototype.push="push;" // concat // // Joins two arrays and returns a new array. // It doesn't alter the original arrays, but be // careful if the arrays contain references to // objects instead of just strings or numbers. // References to objects will be copied as // references. This means that if you change // an object in the original arrays, it will // also be changed in the new array. // function concat(second_array) { var first_array="this.copy();" for (loop="0;" loop<second_array.length; loop++) { first_array[first_array.length]="second_array[loop];" } return first_array; } Array.prototype.concat="concat;" // shift // // Removes the first element of an array and // returns that element. // function shift() { var new_value="this[0];" var orig_length="this.length;" for (loop="0;" loop<this.length-1; loop++) { this[loop]="this[loop+1];" } this.length--; return new_value; } Array.prototype.shift="shift;" // unshift // // Adds an element to the beginning of an array and // returns the new length of the array. // function unshift(the_item) { for (loop="this.length-1" ; loop><this.length; loop++) { random_num="Math.round(Math.random()" * (temp_array.length-1)); new_array[loop]="temp_array[random_num];" temp_array[random_num]="temp_array[temp_array.length-1];" temp_array.length--; } return new_array; } Array.prototype.permute="permute;" // &gt; &lt;/script&gt; </textarea></font></font></p>
    </form>
    </td>
  </tr>
  <tr>
    <td width="155" valign="bottom" bgcolor="#FFFFB5"><font face="宋体"><br>
    </font><font size="2" face="宋体">问题? 建议? 请<a href="mailto:bruce@itc.cn.net">告诉</a>网猴。</font></td>
  </tr>
</table>

<p><font face="宋体"><br>
</font></p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -