实现连缀调用的map方法(prototype)

复制代码 代码如下:

  <script type="text/javascript">

  function SpecialArray(arr){

  this.arr=arr;

  }

  SpecialArray.prototype.map=function(func){

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

  this.arr[i]=func(this.arr[i]); //调用函数,改变arr数组的每个项的值

  }

  return this; //返回自身对象

  }

  var obj=new SpecialArray([ a , b , c ]);

  //可以对obj的arr属性做任何的操作

  alert(obj.map(function(el){return el.toUpperCase()}).arr);

  alert(obj.map(function(el){return el+"!";}).arr);

  </script>