Quantcast
Channel: w3cplus
Viewing all articles
Browse latest Browse all 1557

《献给你,我深爱的ECMAScript》之Array篇

$
0
0

本文主要说说ECMAScript的Array相关

如何判定是否是数组

        //underscore 1.5.1
        //2012-02-22 1.0.0版本 ratbeard增加了对ES5的支持
        //https://github.com/jashkenas/underscore/commit/b774bf7ca9770625c0dffa99d46c14f22fdf7776
        //ratbeard的博客:http://www.mikefrawley.com/work.html
        var nativeIsArray = Array.isArray,
            toString = Object.prototype.toString; 

        _.isArray = nativeIsArray || function(obj){
            return toString.call(obj) == '[object Array]';
        }


        //tangram 2.0.2.5
        //一般做法走toString
        baidu.lang.isArray = function(source){
            return Object.prototype.toString.call(source) == '[object Array]';
        }


        //qwrap 1.1.5
        //最早看到这个设计后,我就主动加入到我的博客了
        //slice截断的:[object Object] --> Object
        //忘了slice的参数,请看这里:http://zhangyaochun.iteye.com/blog/1450716
        function getConstructorName(o){
            if(o != null && o.constructor != null){
                return Object.prototype.toString(o).slice(8,-1);
            }else{
                return '';
            }
        }

        isArray:function(obj){
            return getConstructorName(obj) == 'Array';
        }

    

array的indexOf?


        //kissy 1.3.0
        var indexOf = Array.prototype.indexOf;
        indexOf:indexOf ? 
            function(item,arr){
                return indexOf.call(arr,item);
            } :
            function(item,arr){
                for(var i = 0,len = arr.length;i < len;++i){
                    if(arr[i] === item){
                        return i;
                    }
                }

                return -1;
                
            };


        //tangram 1.5.2.2
        //其实最开始看到第三个参数的时候,当时我还是小惊,没有想到
        baidu.array.indexOf = function(source,match,formIndex){
            var len = source.length,
                iterator = match;

            //可选参数设置默认值的做法    
            fromIndex = fromIndex | 0;
            
            //相比下面qwrap的做法,我喜欢Math.max的设计
            if(formIndex < 0){
                formIdx = Math.max(0,len + fromIndex);
            }    
            for(;fromIndex < len;formIdx++){
                if(formIdx in source && source[formIdx] === match){
                    return formIdx;
                }
            }

            return -1;
        }


        //qwrap
        //qwrap有意思的地方是手册里面在arr.indexOf备注这:如果浏览器已支持indexOf,则会调用浏览器默认的该方法 
        indexOf: function(arr,obj,formIdx){
            var len = arr.length;
            formIdx != 0;
            if(formIdx < 0){
                formIdx += len;
            }

            //我还是喜欢优雅的Math.max(0,len + fromIndex);
            if(formIdx < 0){
                formIdx = 0;
            }

            for(;formIdx < len;formIdx++){
                if(formIdx in arr && arr[formIdx] === obj){
                    return formIdx;
                }
            }

            return -1;
        }
    

array的lastIndexOf

        var lastIndexOf = Array.prototype.lastIndexOf;

        lastIndexOf: (lastIndexOf) ?
            function(item,arr){
                return lastIndexOf.call(arr,item);
            } :
            function(item,arr){
                for(var i=arr.length-1;i>=0;i--){
                    if(arr[i] === item){
                        break;
                    }
                }

                return i;
            };



        //tangram 1.5.2.2
        baidu.array.lastIndexOf = function(source,match,fromIndex){
            var len = source.length;
            fromIndex = fromIndex | 0;

            if(!fromIndex || fromIndex > len){
                fromIndex = len - 1;
            }

            if(fromIndex < 0){
                fromIndex += len;
            }

            for(;fromIndex >= 0;fromIndex --){
                if(fromIndex in source && source[fromIndex] === match){
                    return fromIndex;
                }
            }

            return -1;

        };        
    

如需转载,烦请注明出处:http://www.w3cplus.com/js/ecmascript-lesson-6.html


Viewing all articles
Browse latest Browse all 1557

Trending Articles