我从 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()
&&
不只是返回一个值。它还具有(流控制)效果:仅当其第一个操作数为真时才评估其第二个操作数。也就是说,如果已经是,A && B
则不会运行。(这称为短路评估。)B
A
false
换句话说,
typeof window.WebForm_OnSubmit == "function" && window.WebForm_OnSubmit();
表现得像
if (typeof window.WebForm_OnSubmit == "function")
window.WebForm_OnSubmit();
但略短。
重点是不返回true
或false
(结果被忽略);关键是只有window.WebForm_OnSubmit
在定义为函数时才调用。
相似地,
!bValidSearchTerm && _ClearSearchTerm("");
像这样工作:
if (!bValidSearchTerm)
_ClearSearchTerm("");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句