javascript - 不同的表达式封装成函数

 无石笑_987 发布于 2022-11-18 07:37

有几段相同代码,我想封装成一个函数,请问改怎么封装?

//====第一段代码======

    var cookies = document.cookie;
    var start = cookies.indexOf("authData=");
    if(start == -1){
        console.log("The cookie not found");
    }
    start = cookies.indexOf("=", start) + 1;
    var end = cookies.indexOf(";", start);
    if(end == -1){
        end = cookies.length;
    }
    var authData = unescape(cookies.substring(start, end));
    if(authData == null){
        console.log("The cookie not found");
    }
    else{
        var json = JSON.parse(authData);
        for(var i=0; i< json.data.length;i++){
            if(json.data[i].partentId == $routeParams.addAdminId){
                if(json.data[i].actionName == "提交"){
                    $scope.submitAddAdmin = true;
                }
            }
        }
    }
    

//====第二段代码============

var cookies = document.cookie;

    var start = cookies.indexOf("authData=");
    if(start == -1){
        console.log("The cookie not found");
    }
    start = cookies.indexOf("=", start) + 1;
    var end = cookies.indexOf(";", start);
    if(end == -1){
        end = cookies.length;
    }
    var authData = unescape(cookies.substring(start, end));
    if(authData == null){
        console.log("The cookie not found");
    }
    else{
        var json = JSON.parse(authData);
        for(var i=0; i< json.data.length;i++){
            if(json.data[i].partentId == $routeParams.roleAuthListId){
                if(json.data[i].actionName == "编辑"){
                    $scope.editRoles = true;
                }
                if(json.data[i].actionName == "删除"){
                    $scope.delRoles = true;
                }
            }
        }
    }  
    
    

每段代码只有else 里面的 for循环里面的代码不一样,请问这种怎么封装成一个函数?

2 个回答
  • 只封装获取cookie中authData的数据部分

    function getAuthData(){
        var cookies = document.cookie;
        var start = cookies.indexOf("authData=");
        if(start == -1){
            console.log("The cookie not found");
            return {data:[]}
        }
        start = cookies.indexOf("=", start) + 1;
        var end = cookies.indexOf(";", start);
        if(end == -1){
            end = cookies.length;
        }
        var authData = unescape(cookies.substring(start, end));
        if(authData == null){
            console.log("The cookie not found");
            return {data:[]}
        }
        else{
            return JSON.parse(authData);
        }
    }
    
    var json= getAuthData();
    
    for(var i=0; i< json.data.length;i++){
        if(json.data[i].partentId == $routeParams.addAdminId){
            if(json.data[i].actionName == "提交"){
                $scope.submitAddAdmin = true;
            }
        }
    }
    
    for(var i=0; i< json.data.length;i++){
        if(json.data[i].partentId == $routeParams.roleAuthListId){
            if(json.data[i].actionName == "编辑"){
                $scope.editRoles = true;
            }
            if(json.data[i].actionName == "删除"){
                $scope.delRoles = true;
            }
        }
    }
    
    
    
    2022-11-18 08:56 回答
  • Angular中一般封装成service

    app.factory('getAuthData', function() {
        return function(cb) {
            var cookies = document.cookie;
            var start = cookies.indexOf("authData=");
            if (start == -1) {
                console.log("The cookie not found");
            }
            start = cookies.indexOf("=", start) + 1;
            var end = cookies.indexOf(";", start);
            if (end == -1) {
                end = cookies.length;
            }
            var authData = unescape(cookies.substring(start, end));
            if (authData == null) {
                console.log("The cookie not found");
            } else {
                var json = JSON.parse(authData);
                cb(json)
            }
        }
    })

    在controller里注入就可以使用了

    app.controller('myController',['getAuthData',function(getAuthData){
        getAuthData(function(json){
            
            console.log(json);
        })
    }])
    2022-11-18 08:56 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有