码迷,mamicode.com
首页 > 其他好文 > 详细

Array原生方法

时间:2014-06-28 21:38:30      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:cti   代码   for   c   io   window   

push()末尾推入元素 返回数组长度

pop()末尾弹出元素 返回弹出元素

shift()起始弹出元素 返回弹出元素

unshift()起始推入元素 返回数组长度

代码如:

var arr1 = ["b","c","d","e"];

var arr2 = arr1.push("f");//arr1:["b","c","d","e","f"], arr2:5

var arr3 = arr1.pop();//arr1:["b","c","d","e"], arr3:"f"

var arr4 = arr1.unshift("a");//arr1:["a","b","c","d","e"], arr4:5

var arr5 = arr1.shift();//arr1:["b","c","d","e"], arr5:"a"

 

Ecmascript5有扩展数组原型方法forEach,filter等

if(typeof Array.prototype.forEach !== "function"){

  Array.prototype.forEach = function(fn,thisObj){

      var scope = thisObj || window;

      for(var i=0, len=this.length; i<len; i++){

        fn.call(scope,this[i],[i],this);

      }

  }

};

if(typeof Array.prototype.filter!== "function"){

  Array.prototype.filter= function(fn,thisObj){

      var scope = thisObj || window;

      var a = [];

      for(var i=0, len=this.length; i<len; i++){

        if(!fn.call(scope,this[i],[i],this)){

          continue;

        }

        a.push(this[i])

      }

      return a;

  }

}

 

 

Array原生方法,布布扣,bubuko.com

Array原生方法

标签:cti   代码   for   c   io   window   

原文地址:http://www.cnblogs.com/samKR/p/3794493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!