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

stackedMap by angular

时间:2015-10-14 10:40:28      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

angular.module(‘demo.stackedMap‘,[])
// 提供一个服务
.factory(‘$$stackedMap‘,function(){
    return {
        createNew:function(){
            var stack = [];
            
            return {
                // 添加方法
                add:function(key,value){
                    stack.push({
                        key:key,
                        value:value
                    })
                },
                
                get:function(key){
                    for(var i = 0;i<stack.length;i++){
                        if(key === stack[i].key){
                            return stack[i];
                        }
                    }
                },
                
                keys:function(){
                    var keys = [];
                    for(var i = 0;i<stack.length;i++){
                        keys.push(stack[i].key);
                    }
                    return keys;
                },
                
                top:function(){
                    return stack[stack.length -1];
                },
                
                remove:function(key){
                    var idx = -1;
                    for(var i = 0;i<stack.length;i++){
                        if(key = stack[i].key){
                            idx = i;
                            break;
                        }
                    }
                    return stack.splice(idx,1)[0];
                },
                
                removeTop:function(){
                    return stack.splice(stack.length-1,1)[0];
                },
                
                length:function(){
                    return stack.length;
                }
            }
        }
    }
});






stackedMap by angular

标签:

原文地址:http://my.oschina.net/bosscheng/blog/516893

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