javascript原型遍历数组

来源:互联网 发布:淘宝买家秀福利地址 编辑:程序博客网 时间:2024/05/16 06:49
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>遍历数组</title></head><body>    <script>    var arr = [1,2,3,[4,[5]]];    Array.prototype.each = function(fn){        try{            this.i || (this.i = 0);            if(this.length > 0 && fn.constructor == Function){                while(this.i < this.length){                    var e = this[this.i];                    if(e && e.constructor == Array){                        e.each(fn);                    }else{                        // fn.apply(e,[e]);                        fn.call(e,e);                    }                    this.i++;                }                this.i = null;            }        }catch(err){            alert(err)        }    }    arr.each(function(item){        alert(item)    })    </script></body></html>
原创粉丝点击