webcode 发表于 2013-1-29 09:27:01

几个常用的Javascript函数

// *******************************************************
// replacements for unsupported array functions (because arrayname.push(var)
// and arrayname.pop() are not implemented in IE until version 5.5)
function thearrayisgood(thearray,i) {
   if ((thearray == "undefined") || (thearray == "") || (thearray == null))
   return false;
   else
   return true;
}

function getarraysize(thearray) {
// replacement for arrayname.length property
for (i = 0; i < thearray.length; i++) {
      if ((thearray == "undefined") || (thearray == "") || (thearray == null))
      return i;
      }
return thearray.length;
}

function arraypush(thearray,value) {
// replacement for arrayname.push(value)
thearraysize = getarraysize(thearray);
thearray = value;
return thearray;
}

function arraypop(thearray) {
// replacement for arrayname.pop()
thearraysize = getarraysize(thearray);
retval = thearray;
delete thearray;
thearray.length--; <<==我加了这句,很关键,否则出错
return retval;
}

// *******************************************************

另外给出网上几个老外写的
function Array_pop() {
var response = this
this.length--
return response
}

if (typeof(Array.prototype.pop) == "undefined") {
Array.prototype.pop = Array_pop
}

function Array_push() {
var A_p = 0
for (A_p = 0; A_p < arguments.length; A_p++) {
this = arguments
}
return this.length
}

if (typeof Array.prototype.push == "undefined") {
Array.prototype.push = Array_push
}
页: [1]
查看完整版本: 几个常用的Javascript函数