javascript - 不同的表达式封装成函数
阿神
阿神 2017-04-10 17:25:35
[JavaScript讨论组]

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

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

    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)
PHP中文网

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);
    })
}])
迷茫

只封装获取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;
        }
    }
}


热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号