JavaScript函数-自定义小部件- Appcelerator合金

JavaScript functions - Custom widget - Appcelerator alloy

本文关键字:Appcelerator 合金 自定义 函数 JavaScript 小部      更新时间:2023-09-26

我在设计自定义js小部件时遇到了一些问题,下面是代码

function SomeCustomWidget() {
    //private variables
    var referenceOfThis = this;
    var sideMenuWidth = 200;
    var selectedBgColor = '#8EBA04';
    function createWidget(){
        var containerView = //initialization
        var upperPartOfView = createUpperPartOfWidget();
        Ti.API.info("LOG2  This log is not getting printed");
        containerView.add(upperPartOfView);
        var lowerPartOfView = createLowerPartOfWidget();
        containerView.add(lowerPartOfView);
    }

    //Private methods
    function createUpperPartOfWidget(e) {
        var widgetUpperPart = //initialization codehere
        Ti.API.info("LOG1  This log is getting printed");
        return widgetUpperPart;
    }
    function createLowerPartOfWidget(e) {
        var widgetLowerPart = //initialization codehere
        return widgetLowerPart;
    }
    //Previlage method
    this.highlightWidget = function(e) {
        //access private variables and does somestuff
    };
}
module.exports = SomeCustomWidget;

我有几个问题

  1. 是构造函数内部的私有方法自动执行,createWidget在我的情况下

2。调用私有函数后的代码没有被执行,(在我的情况下,LOG1被打印,LOG2没有,代码中是否有任何错误)

这是创建自定义视图的正确方法吗,请建议!

您正在谈论Alloy和Widgets,但您显示的代码是CommonJS模块,而不是https://appcelerator.github.io/appc-docs/latest/#!/指导/Alloy_Widgets

不,当你调用SomeCustomWidget()或做new SomeCustomWidget() JS不会运行createWidget()自己。