在 Durandal 视图模型中返回函数和返回对象有什么区别?

What is the difference between returning a function and returning an object in a Durandal viewmodel?

我正在考虑在我的应用程序中实现向导类型系统,并查看 GitHub 上 dfiddle-2.0 项目的第一个向导示例。尽管步骤视图模型都是函数,但我正在尝试理解原因。

以下是 dfiddle 用于 index.js 向导的内容:

define(['durandal/activator', './step1', './step2', './step3', 'knockout'], function( activator, Step1, Step2, Step3, ko ) {

    var steps = [new Step1(), new Step2(), new Step3()];
    var step = ko.observable(0);
    var activeStep = activator.create();
    var stepsLength = steps.length;

    var hasPrevious = ko.computed(function() {
        return step() > 0;
    });

    var hasNext = ko.computed(function() {
        return (step() < stepsLength - 1);
    });

    // Start with first step
    activeStep(steps[step()]);

    return {
        showCodeUrl: true,
        steps: steps,
        step: step,
        activeStep: activeStep,
        next: next,
        previous: previous,
        hasPrevious: hasPrevious,
        hasNext: hasNext
    };

    function next () {
        if ( step() < stepsLength ) {
            step(step() + 1);
            activeStep(steps[step()]);
        }
    }

    function previous () {
        if ( step() > 0 ) {
            step(step() - 1);
            activeStep(steps[step()]);
        }
    }

});

这是它用于 step1.js

的内容
define(function() {

    return function() {
        this.name = 'Step 1';
        this.s1one = 'Unique to' + this.name;
        this.s1two = 'Another property unique to' + this.name;
    };

});

这是我目前在 index.js.

中使用的内容
define(['knockout'],
    function (ko) {
        var rootPath = "viewmodels/wizards/steps/";
        var steps = ["step1", "step2", "step3"];
        var step = ko.observable(0);
        var activeStep = ko.observable(); 
        var stepLength = steps.length;

        var hasPrevious = ko.computed(function () { return step() > 0 });
        var hasNext = ko.computed(function () { return step() < stepLength - 1 });

        var activate = function () {
            return activeStep(rootPath + steps[step()]);
        };

        return {
            steps: steps,
            step: step,
            activeStep: activeStep,
            next: next,
            previous: previous,
            hasPrevious: hasPrevious,
            hasNext: hasNext,
            activate: activate
        }

        function next() {
            if (hasNext()) {
                step(step() + 1);
                activeStep(rootPath + steps[step()]);
            }
        }

        function previous() {
            if (hasPrevious()) {
                step(step() - 1);
                activeStep(rootPath + steps[step()]);
            }
        }
    });

还有我的step1.js

define(function () {
    var name = ko.observable("Step 1");
    var s1one = ko.observable("Unique to " + name());
    var s1two = ko.observable("Another property unique to " + name());
    var returnVm = {
        name: name,
        s1one: s1one,
        s1two: s1two
    };

    return returnVm;
});

绑定相同,那么这两种方法有何不同?只返回一个对象而不是使用函数,我会失去什么?

差异很小,但很重要。 return 对象的模块是单例。同一对象将在依赖它的所有其他模块之间共享。 return 函数的模块称为构造函数。依赖模块将使用 new 关键字实例化此构造函数。因此,每个实例都是独一无二的。

以下是从 Durandal documentation 中收集到的更多信息:

A module's define is only exeucted once, at the time the module is first required. As a result, if you return an object instance, you have created a singleton which will stay in memory for the lifetime of your application. If this is not desired, return a constructor function to retain greater control of the lifetime of your objects by allowing consumers to create/release them as needed.

在您的示例中,您没有丢失任何东西。两种方法都有效。哪个更正确取决于很多事情。如果您不需要每次都需要模块的唯一实例,那么单例是最佳选择。但是,如果说您需要同一个对话框模块的多个实例,但每个实例都有自己的数据,构造函数是可行的方法。

希望对您有所帮助。