下面的语句之间的最终区别是什么,以确定cts.search是否返回有效的文档?
1. if (!fn.empty(acctDoc)) {....}
2. if (!fn.exists(acctDoc)) {....}
3. if (acctDoc !== null || acctDoc !== "") {...}
我的经验是,No.3在各个方面都有效。
if (!fn.empty(acctDoc)) {....}
如果acctDoc
if (!fn.exists(acctDoc)) {....}
如果acctDoc
if (acctDoc !== null || acctDoc !== "") {...}
如果acctDoc
不是null
或它不是一个空字符串,则返回true。如果$ arg的值为空序列,则该函数返回true;否则,该函数返回true。否则,该函数返回false。
如果$ arg的值不是空序列,则该函数返回true;否则,该函数返回true。否则,该函数返回false。
使用此测试功能:
function test(acctDoc) {
const results = [];
if (!fn.empty(acctDoc)) { results.push("not empty") };
if (!fn.exists(acctDoc)) { results.push("does not exist") };
if (acctDoc !== null || acctDoc !== "") { results.push("not null or is not empty string") };
return results;
}
test("test")
返回["not empty", "not null or is not empty string"]
test("")
返回["not empty", "not null or is not empty string"]
test(null)
返回["does not exist", "not null or is not empty string"]
test(fn.head(fn.doc()))
返回["not empty", "not null or is not empty string"]
test(fn.doc(fn.string(xdmp.random())))
返回["does not exist", "not null or is not empty string"]