Titanium 钛项目中用Alloy和CommonJS组织JS代码

Titanium 钛项目中用Alloy和CommonJS组织JS代码,titanium,appcelerator,commonjs,titanium-alloy,Titanium,Appcelerator,Commonjs,Titanium Alloy,我正在用钛合金学习制作iPhone/Android应用程序。我使用的是Alloy MVC框架。我以前从未使用过javascript,除了使用HTML中的简单脚本来访问DOM或类似的东西之外,因此我以前从未需要构造代码 现在,对于Tianium,我必须使用很多JS代码,我正在寻找构建代码的方法。基本上我找到了3种方法:原型、名称空间和函数内部的函数 每种方法的简单示例: 原型: NavigationController = function() { this.windowStack = [

我正在用钛合金学习制作iPhone/Android应用程序。我使用的是Alloy MVC框架。我以前从未使用过javascript,除了使用HTML中的简单脚本来访问DOM或类似的东西之外,因此我以前从未需要构造代码

现在,对于Tianium,我必须使用很多JS代码,我正在寻找构建代码的方法。基本上我找到了3种方法:原型、名称空间和函数内部的函数

每种方法的简单示例:

原型

NavigationController = function() {
    this.windowStack = [];
};

NavigationController.prototype.open = function(windowToOpen) {
    //add the window to the stack of windows managed by the controller
    this.windowStack.push(windowToOpen);

    //grab a copy of the current nav controller for use in the callback
    var that = this;
    windowToOpen.addEventListener('close', function() {
        if (that.windowStack.length > 1)
        {
            that.windowStack.pop();
        }
    });

    if(Ti.Platform.osname === 'android') {
        windowToOpen.open();
    } else {
        this.navGroup.open(windowToOpen);
    }
};

NavigationController.prototype.back = function(w) {
    //store a copy of all the current windows on the stack
    if(Ti.Platform.osname === 'android') {
        w.close();
    } else {
        this.navGroup.close(w);
    }
};

module.exports = NavigationController;
function foobar(){
    this.foo = function(){
        console.log('Hello foo');
    }

    this.bar = function(){
        console.log('Hello bar');
    }
}

// expose foobar to other modules
exports.foobar = foobar;
将其用作:

var NavigationController = require('navigator');
var navController = new NavigationController();
var ui = require('navigation');
var nav = ui.createNavigatorGroup();
var foobar = require('foobar').foobar
var test = new foobar();

test.bar(); // 'Hello bar'
名称空间(或者我认为是这样的,因为使用了me={}):

将其用作:

var NavigationController = require('navigator');
var navController = new NavigationController();
var ui = require('navigation');
var nav = ui.createNavigatorGroup();
var foobar = require('foobar').foobar
var test = new foobar();

test.bar(); // 'Hello bar'
函数内部的函数

NavigationController = function() {
    this.windowStack = [];
};

NavigationController.prototype.open = function(windowToOpen) {
    //add the window to the stack of windows managed by the controller
    this.windowStack.push(windowToOpen);

    //grab a copy of the current nav controller for use in the callback
    var that = this;
    windowToOpen.addEventListener('close', function() {
        if (that.windowStack.length > 1)
        {
            that.windowStack.pop();
        }
    });

    if(Ti.Platform.osname === 'android') {
        windowToOpen.open();
    } else {
        this.navGroup.open(windowToOpen);
    }
};

NavigationController.prototype.back = function(w) {
    //store a copy of all the current windows on the stack
    if(Ti.Platform.osname === 'android') {
        w.close();
    } else {
        this.navGroup.close(w);
    }
};

module.exports = NavigationController;
function foobar(){
    this.foo = function(){
        console.log('Hello foo');
    }

    this.bar = function(){
        console.log('Hello bar');
    }
}

// expose foobar to other modules
exports.foobar = foobar;
将其用作:

var NavigationController = require('navigator');
var navController = new NavigationController();
var ui = require('navigation');
var nav = ui.createNavigatorGroup();
var foobar = require('foobar').foobar
var test = new foobar();

test.bar(); // 'Hello bar'
现在我的问题是:保持代码干净和清晰哪个更好?似乎原型是清晰易懂的。名称空间让我有点困惑,但只需要执行初始函数就可以“可用”(声明它时不使用new,我想是因为它返回对象?名称空间?“me”)。最后,函数内部的函数与上一个类似,因此我不知道确切的区别,但是只导出主函数并使所有内部函数可供以后使用非常有用

也许最后两种可能性是一样的,我把概念搞乱了

请记住,我正在寻找一种很好的方法来构造代码,并使其他模块和自己的模块都可以使用函数


非常感谢您的澄清。

在他们发布的示例中,Appcelerator似乎遵循了非原型方法。您可以在他们发布的示例中看到:

我已经看到了很多不同的方法来构建钛合金的应用程序。自从Alloy之后,我发现跟随开发团队的例子对我很有帮助

话虽如此,在我看来,所有这些仍在解释之中,并对变革和社区发展持开放态度。在Alloy之前,有一些关于构建应用程序的很棒的社区建议,我相信Alloy仍然是开放的。通常,当我找到某人的示例代码时,我看到他们用它做了一些事情,似乎比我想象的更好地组织了代码。这似乎使它变得容易一些

我认为你应该以一种对你有意义的方式来构造你的应用程序。您可能会偶然发现使用Alloy开发应用程序的更好、更简单的方法,因为您正以批判性的眼光看待它


我还没有找到很多广泛的合金示例,但Field Service应用程序对我来说很有意义。除了MVC之外,它们还很好地分离了应用程序中的元素。看看吧。

嗨,马丁。谢谢你的回复。我对您推荐的项目做了一点介绍,它似乎使用了“函数中的函数”方法。正如你所说,最重要的是对选择感到舒适。现在我正在使用原型,我觉得很清楚,但会看看是否有更多的意见。