六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 28|回复: 0

几个常用的Javascript函数

[复制链接]

升级  75.55%

819

主题

819

主题

819

主题

探花

Rank: 6Rank: 6

积分
2511
 楼主| 发表于 2013-1-29 09:27:01 | 显示全部楼层 |阅读模式
// *******************************************************
// 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[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))
     return false;
   else
     return true;
}

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

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

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

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

另外给出网上几个老外写的
function Array_pop() {
var response = this[this.length - 1]
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[this.length] = arguments[A_p]
}
return this.length
}

if (typeof Array.prototype.push == "undefined") {
Array.prototype.push = Array_push
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表