简体   繁体   English

遇到函数javascript中的真假语句,有什么意义?

[英]Came across True false statements in function javascript, what is the point?

I was ripping apart some code from sharepoint and noticed that they have random true false statements in their functions. 我正在从sharepoint中删除一些代码并注意到它们在函数中有随机的真假语句。 My first thought was that they would stop the next statement from being ran, but when I made a simple test, the console log showed it ran no matter what. 我的第一个想法是他们会阻止下一个语句被运行,但是当我进行一个简单的测试时,控制台日志显示它无论如何运行。

Could someone let me know what this is for, what the point is, and why you would do it. 有人能让我知道这是为了什么,重点是什么,为什么你会这样做。

here is an example I came across, and will post my test. 这是我遇到的一个例子,并将发布我的测试。

 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()
            }
        }
    }
}

and here was my test, the console window was hitting all the console.logs that I thought it wouldn't 这是我的测试,控制台窗口击中了我认为不会的所有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()

&& doesn't just return a value. &&不只是返回一个值。 It also has a (flow control) effect: Its second operand is only evaluated if its first operand is true. 它还具有(流量控制)效果:仅在第一个操作数为真时才计算其第二个操作数。 That is, A && B doesn't run B if A is already false . 也就是说,如果A已经为false ,则A && B不会运行B (This is known as short-circuit evaluation .) (这称为短路评估 。)

In other words, 换一种说法,

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

behaves like 表现得像

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

but is slightly shorter. 但稍短。

The point is not to return true or false (the result is ignored); 要点不是返回truefalse (结果被忽略); the point is to only call window.WebForm_OnSubmit if it is defined as a function. 关键是只调用window.WebForm_OnSubmit如果它被定义为一个函数。

Similarly, 同样的,

!bValidSearchTerm && _ClearSearchTerm("");

works like: 的作用如下:

if (!bValidSearchTerm)
    _ClearSearchTerm("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM