使用Javascript对象创建服务

使用Javascript对象创建服务,javascript,angularjs,Javascript,Angularjs,我正在尝试向JS对象添加函数,该对象将用作单例服务 angular .module('app.steps') .factory('createStepsService', createStepsService); createStepsService.$inject = []; /* @ngInject */ function createStepsService() { var steps; var

我正在尝试向JS对象添加函数,该对象将用作单例服务

angular
        .module('app.steps')
        .factory('createStepsService', createStepsService);

    createStepsService.$inject = [];

    /* @ngInject */
    function createStepsService() {
        var steps;

        var service = {
            newSteps: function (current_step, total_steps) {
                if (!steps) {                   
                    return new Steps(current_step, total_steps);
                }
            }            
        };
        return service;

        function Steps(current_step, total_steps) {
            this.c_step = current_step;
            this.t_step = total_steps;            
        }

        Steps.prototype = {
            addSteps: function (num) {
                this.c_step += num;
            },
            setLastStep: function () {
                this.lastStep = this.c_step = this.t_step;
            }
        };        
    }
当我从控制器运行此线路时,我无法访问 addSteps/setLastStep方法

vm.createStepsService = createStepsService.newSteps(1, 3);
为什么我看不到这些方法?他们是被创造出来的吗


谢谢。

您的问题是您正在创建
步骤。prototype
return
语句之后,所以它永远不会被读取。

您的问题是您正在创建
步骤。prototype
return
语句之后,所以它永远不会被读取。

您的
步骤。prototype
代码永远不会运行

这是因为它出现在
返回
之后

将代码的顺序更改为:

/* @ngInject */
function createStepsService() {
    var steps;

    function Steps(current_step, total_steps) {
        this.c_step = current_step;
        this.t_step = total_steps;            
    }

    Steps.prototype = {
        addSteps: function (num) {
            this.c_step += num;
        },
        setLastStep: function () {
            this.lastStep = this.c_step = this.t_step;
        }
    };

    var service = {
        newSteps: function (current_step, total_steps) {
            if (!steps) {                   
                return new Steps(current_step, total_steps);
            }
        }            
    };       

    return service; 
}

您可以在返回之前声明函数的原因是。

您的
步骤。prototype
代码从未运行过

这是因为它出现在
返回
之后

将代码的顺序更改为:

/* @ngInject */
function createStepsService() {
    var steps;

    function Steps(current_step, total_steps) {
        this.c_step = current_step;
        this.t_step = total_steps;            
    }

    Steps.prototype = {
        addSteps: function (num) {
            this.c_step += num;
        },
        setLastStep: function () {
            this.lastStep = this.c_step = this.t_step;
        }
    };

    var service = {
        newSteps: function (current_step, total_steps) {
            if (!steps) {                   
                return new Steps(current_step, total_steps);
            }
        }            
    };       

    return service; 
}

您可以在返回之前声明函数的原因是。

在AngularJS中,服务是单例对象,每个应用程序只实例化一次。

factory()方法是创建和配置服务的快速方法。 它提供函数的返回值,即需要创建一个对象,向其添加属性,然后返回相同的对象

对于ex:

angular
.module('myApp',[])
.factory("createStepService", function(){
    var stepServiceObj = {};
    var c_step = 0;
    var t_steps = 0;
    var last_step = 0;

    stepServiceObj.setCurrentStep = function(current_step){
        c_step = current_step;
        console.log('c_step1: ',c_step);
    };
    stepServiceObj.getCurrentStep = function(){
        return c_step;
    };

    stepServiceObj.setTotalStep = function(total_steps){
        t_steps = total_steps;
    };
    stepServiceObj.getTotalStep = function(){
        return t_steps;
    };

    stepServiceObj.setLastStep = function(){
        last_step = c_step = t_step;
    };
    stepServiceObj.getLastStep = function(){
        return last_step;
    };

    stepServiceObj.addSteps = function(num){
        return c_step += num;
    };

    return stepServiceObj;
});

在AngularJS中,服务是单例对象,每个应用只实例化一次。

factory()方法是创建和配置服务的快速方法。 它提供函数的返回值,即需要创建一个对象,向其添加属性,然后返回相同的对象

对于ex:

angular
.module('myApp',[])
.factory("createStepService", function(){
    var stepServiceObj = {};
    var c_step = 0;
    var t_steps = 0;
    var last_step = 0;

    stepServiceObj.setCurrentStep = function(current_step){
        c_step = current_step;
        console.log('c_step1: ',c_step);
    };
    stepServiceObj.getCurrentStep = function(){
        return c_step;
    };

    stepServiceObj.setTotalStep = function(total_steps){
        t_steps = total_steps;
    };
    stepServiceObj.getTotalStep = function(){
        return t_steps;
    };

    stepServiceObj.setLastStep = function(){
        last_step = c_step = t_step;
    };
    stepServiceObj.getLastStep = function(){
        return last_step;
    };

    stepServiceObj.addSteps = function(num){
        return c_step += num;
    };

    return stepServiceObj;
});

效果很好。谢谢@巴迪加德很高兴能帮上忙!了解变量起重和功能起重工作的方式是值得了解的。这将帮助您编写更好的代码。效果非常好。谢谢@巴迪加德很高兴能帮上忙!了解变量起重和功能起重工作的方式是值得了解的。这将帮助您编写更好的代码。