【问题标题】:Can I declare a static private function in typescript?我可以在打字稿中声明一个静态私有函数吗?
【发布时间】:2012-11-08 05:20:54
【问题描述】:

我有以下代码:

module Dialog {
    export class Modal {
        static createAccessModal(link: Link) {
            createModal(link);
        }
        static createAdminModal(link: Link) {
            link.Modal.MaxHeight = 600;
            link.Modal.Width = false;
            createModal(link);
        }
        static private createModal(link: Link) {

            ...
        }
    }
}

我不想被允许直接调用 createModal,所以我尝试将其设为私有。当我使用智能感知时,它会显示一个小锁符号,但是当我使用它时它不会给出任何错误。有没有其他方法可以做到这一点。这是我调用函数的方式:

Dialog.Modal.createAccessModal(link); // I want this to be allowed
Dialog.Modal.createModal(link); // I don't want this to be allowed

顺便说一句,我对所有事情都使用静态函数,因为这些函数除了在屏幕上创建对象之外什么都不做,然后对象会照顾自己,因为它们有自己的提交按钮等。这样做是否合理?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:
    module Dialog {
        export module Modal {
            export function createAccessModal(link: Link) {
                createModal(link);
            }
            export function createAdminModal(link: Link) {
                link.Modal.MaxHeight = 600;
                link.Modal.Width = false;
                createModal(link);
            }
            function createModal(link: Link) {
    
                ...
            }
        }
    }
    

    【讨论】:

    • 感谢您的快速回答和好的建议。现在我想知道使用具有所有静态函数的类对我有什么好处吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2013-01-22
    相关资源
    最近更新 更多