【问题标题】:Came across True false statements in function javascript, what is the point?在函数 javascript 中遇到 True false 语句,有什么意义?
【发布时间】:2019-06-01 16:24:07
【问题描述】:

我从 sharepoint 中剥离了一些代码,并注意到它们的函数中有随机的真假语句。我的第一个想法是他们会阻止下一条语句的运行,但是当我做了一个简单的测试时,控制台日志显示它无论如何都运行了。

有人可以告诉我这是做什么用的,有什么意义,以及为什么要这样做。

这是我遇到的一个示例,将发布我的测试。

 function _SubmitFormPost(a, d, c) {
    if (typeof MSOWebPartPageFormName != "undefined") {

        var b = document.forms[MSOWebPartPageFormName];

        if (null != b)
        {
            if (d != undefined && d == true || typeof b.onsubmit == "undefined" || b.onsubmit == null || b.onsubmit() != false) {
                //HERE IS A RANDOM EXAMPLE OF T/F 
                typeof window.WebForm_OnSubmit == "function" && window.WebForm_OnSubmit();
                if (ajaxNavigate.get_search().match(new RegExp("[?&]IsDlg=1")) != null)
                a += a.indexOf("?") == -1 ? "?IsDlg=1" : "&IsDlg=1";
                if (FV4UI())
                try {
                    var e = SP.Ribbon.PageManager.get_instance().get_ribbon().get_selectedTabId();
                    if (Boolean(e)) {
                        a = StURLSetVar2(a, "InitialTabId", escapeProperly(e));
                        a = StURLSetVar2(a, "VisibilityContext", "WSSTabPersistence")
                    }
                } catch (f) {}
                if (c != undefined && c == true) {
                    a = DemoteIntoFormBody(b, a, "owsfileref");
                    a = DemoteIntoFormBody(b, a, "NextUsing")
                }
                b.action = STSPageUrlValidation(a);
                b.method = "POST";
                if (isPortalTemplatePage(a))
                b.target = "_top";
                !bValidSearchTerm && _ClearSearchTerm("");
                b.submit()
            }
        }
    }
}

这是我的测试,控制台窗口正在访问所有我认为不会的 console.logs

function s(){
    console.log('in s')
    //is true
    if(1==1){
        //is true
        if(2==2){
            console.log(' if 2==2 and eval of t/f is ',typeof(1) =="function")
            //is false -- expexted to skip console.log
            typeof(1) =="function"
            console.log('2 is 2')
        }
        //is true
        2==3
        if(3==3){
            console.log('3==3')
        }
    }
}
s()

【问题讨论】:

    标签: javascript function boolean


    【解决方案1】:

    && 不只是返回一个值。它还具有(流控制)效果:仅当其第一个操作数为真时才评估其第二个操作数。也就是说,如果A 已经是false,则A && B 不会运行B。 (这被称为short-circuit evaluation。)

    换句话说,

    typeof window.WebForm_OnSubmit == "function" && window.WebForm_OnSubmit();
    

    表现得像

    if (typeof window.WebForm_OnSubmit == "function")
        window.WebForm_OnSubmit();
    

    但略短。

    重点是不要返回truefalse(结果被忽略);关键是如果window.WebForm_OnSubmit 被定义为函数,则只调用它。

    同样,

    !bValidSearchTerm && _ClearSearchTerm("");
    

    像这样工作:

    if (!bValidSearchTerm)
        _ClearSearchTerm("");
    

    【讨论】:

    • 非常感谢您的回答,我在这里或谷歌上都找不到。我是自学的,所以我知道会错过很多。这个约定有名字吗?
    • @user2620493 我不知道它是否有名字,但我很确定它首先在 Lisp 中使用,然后传播到 Perl,并从那里传播到 JavaScript。但就我目前所见,人们通常不会手动编写这样的 JavaScript 代码。它主要由代码压缩器和混淆器生成。
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 2017-02-07
    • 2013-11-10
    • 2013-04-23
    • 1970-01-01
    • 2012-07-06
    相关资源
    最近更新 更多