Javascript can';t接入模块';人的属性或方法

Javascript can';t接入模块';人的属性或方法,javascript,titanium,commonjs,Javascript,Titanium,Commonjs,我似乎无法从任何其他模块访问FotoAlbum的viewPort属性,也无法运行试图添加到FotoAlbum的任何函数。我对CommonJS完全陌生,所以这可能是一个简单的修复,但我现在没有看到它 function FotoAlbum(app) { var self = Ti.UI.createWindow( { title : 'Fotoalbum', backgroundColor : '#fff', layout : 'ver

我似乎无法从任何其他模块访问FotoAlbum的viewPort属性,也无法运行试图添加到FotoAlbum的任何函数。我对CommonJS完全陌生,所以这可能是一个简单的修复,但我现在没有看到它

function FotoAlbum(app)
{
    var self = Ti.UI.createWindow(
    {
        title : 'Fotoalbum',
        backgroundColor : '#fff',
        layout : 'vertical',
        height : 'auto',
        width : 'auto'
    });

    this.t=3;

    self.add(app.Navigation.getNavigationBar(app));

    this.viewPort = Ti.UI.createView(
    {
        top : '0dp',
        left : '0dp',
        width : '100%',
        height : 'auto',
        backgroundColor : 'white'
    });

    var label = Ti.UI.createLabel(
    {
        text : 'Foto album'
    });
    this.viewPort.add(label);
    self.add(this.viewPort);

    return self;
}
module.exports = FotoAlbum;

引用函数的上下文
FotoAlbum
,但您使用此函数创建并返回视图,这就是您无法访问函数上下文以及附加到它的属性的原因

因此,要实现这一点,只需将方法和属性附加到视图本身,如下所示:

function FotoAlbum(app) {
    var self = Ti.UI.createWindow();
    // Attach to the view
    self.t=3;
    self.add(app.Navigation.getNavigationBar(app));
    self.viewPort = Ti.UI.createView();

    var label = Ti.UI.createLabel({
        text : 'Foto album'
    });
    self.viewPort.add(label);
    self.add(self.viewPort);

    return self;
}
module.exports = FotoAlbum;
编辑:
忘了提及,不要将对象附加到视图,然后更改该对象的属性,因为javascript代理在Tianium中是如何工作的。此引用函数的上下文
FotoAlbum
,但您使用此函数创建并返回一个视图,这就是为什么您无法访问函数上下文,因此无法访问附加到它的属性

因此,要实现这一点,只需将方法和属性附加到视图本身,如下所示:

function FotoAlbum(app) {
    var self = Ti.UI.createWindow();
    // Attach to the view
    self.t=3;
    self.add(app.Navigation.getNavigationBar(app));
    self.viewPort = Ti.UI.createView();

    var label = Ti.UI.createLabel({
        text : 'Foto album'
    });
    self.viewPort.add(label);
    self.add(self.viewPort);

    return self;
}
module.exports = FotoAlbum;
编辑:
忘了提及,不要将对象附加到视图,然后更改该对象的属性,因为javascript代理在Tianium中如何工作,这将不起作用。

好的,谢谢你的救命恩人,尽管你被允许这样做。好的,谢谢你的救命恩人,尽管你被允许这样做。