当前位置:Gxlcms > JavaScript > 实现连缀调用的map方法(prototype)

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

代码如下:
<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>

人气教程排行