简体   繁体   English

当没有方法的返回值时,为什么 javaScript 的可选链接语法不适用于空值合并运算符?

[英]Why javaScript's Optional Chaining Syntax does not work with Nullish Coalescing Operator when there is no method's return value?

I"m trying the new JavaScript Optional Chaining syntax and it seems that it has an issue with methods which has no return value. For example:我正在尝试新的 JavaScript 可选链接语法,它似乎与没有返回值的方法有关。例如:

I have a restaurant object with order method in it:我有一家餐厅 object,其中包含订购方法:

const restaurant = {
order(i, k) {
    console.log(`I was here with ${i} and ${k}`);
   // does not return value
  }
};

Now if i try to check if the order method exist inside restaurant object like below, it returns 'undefiend':现在,如果我尝试检查餐厅 object 中是否存在订购方法,如下所示,它会返回“undefiend”:

const orderCheck = restaurant.order?.(0, 1);
console.log(orderCheck ); // it returns undefined

Now the problem is, if i use above case with Nullish Coalescing Operator like below, it does not work:现在的问题是,如果我将上述情况与 Nullish Coalescing Operator 一起使用,如下所示,它不起作用:

console.log(restaurant.order?.(0, 1) ?? 'Method does not exist');

Output: 
I was here with 0 and 1
Method does not exist

ISSUE: Now the issue in above statement is, the optional chaining syntax even finds the order method & prints "I was here with 0 and 1" but still returns 'undefiend' which makes Nullish Coalescing Operator to proceed and even it prints 'Method does not exist' too as default value?问题:现在上面语句中的问题是,可选的链接语法甚至可以找到排序方法并打印“I was here with 0 and 1”但仍然返回“undefiend”,这使得 Nullish Coalescing Operator 继续,甚至打印“Method does”不存在'也作为默认值?

Now, if i allow the order method to return a value then it works fine with Nullish Coalescing Operator and it does not proceed.现在,如果我允许 order 方法返回一个值,那么它可以与 Nullish Coalescing Operator 一起正常工作并且它不会继续。

 const restaurant = {
    order(i, k) {
        console.log(`I was here with ${i} and ${k}`);
        return '1';
      }
    };

If check again:如果再次检查:

console.log(restaurant.order?.(0, 1) ?? 'Method does not exist');

Output: 
I was here with 0 and 1

Is it necessary for optional chaining syntax to expect a return value other than nullish values from a method in order to check its availability?可选链接语法是否有必要期望方法的返回值不是空值以检查其可用性?

In above case, the order method does exist then How to prevent Nullish Coalescing Operator to not to proceed for the default 'Method does not exist'?在上述情况下,订单方法确实存在,那么如何防止 Nullish Coalescing Operator 不继续处理默认的“方法不存在”?

I'm testing it with modern browsers (Chrome & firfox) and i have also enabled the javaScript experimental features in Chrome but it still work the same above way.我正在使用现代浏览器(Chrome 和 firfox)对其进行测试,我还在 Chrome 中启用了 javaScript 实验性功能,但它仍然以上述方式工作。

Any thoughts would be helpful?有什么想法会有所帮助吗? Thanks谢谢

This is not related to optional chaining.这与可选链接无关。

The Nullish Operator returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand. Nullish Operator符在其左侧操作数为 null 或未定义时返回其右侧操作数,否则返回其左侧操作数。

Therefore, the optional chaining is already working as the function exists but its return value is undefined , hence the operator goes to the right part.因此,可选链接已经在工作,因为 function 存在但它的返回值是undefined ,因此运算符转到正确的部分。

Note:笔记:

  • the log inside the function is showing as it's getting executed first but no value is being returned. function 中的日志显示为首先执行但没有返回任何值。
  • if function didn't exist, in this case it will also compute to undefined due to the optional chaining so the right side of the nullish operator would be taken as well如果 function 不存在,在这种情况下,由于optional chaining ,它也将计算为undefined ,因此nullish operator的右侧也将被采用

 let obj = {}; console.log("function doesn't exist", obj?.foo?.(), obj?.foo?.()?? 'right'); obj = { foo: () => {} }; console.log("function exists but doesn't return", obj?.foo?.(), obj?.foo?.()?? 'right'); obj = { foo: () => { return 'left'; } }; console.log("function exists and returns", obj?.foo?.(), obj?.foo?.()?? 'right');

Edit: to prevent Nullish Coalescing Operator to not proceed for the default 'Method does not exist' Even the method exist编辑:防止 Nullish Coalescing Operator 不继续默认的“方法不存在”即使方法存在

 obj = { foo: () => {} }; console.log(obj?.foo && typeof obj.foo === 'function'? obj.foo(): 'right');

With restaurant.order?.(0, 1) you check if the method exists, and if it does, then call it and return the result of the call.使用restaurant.order?.(0, 1)检查该方法是否存在,如果存在,则调用它并返回调用结果。 In your case, order(0, 1) returns undefined, therefore restaurant.order?(0, 1) also returns undefined.在您的情况下, order(0, 1)返回未定义,因此restaurant.order?(0, 1)也返回未定义。

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

相关问题 OPTIONAL CHAINING 和 NULLISH COALESCING 运算符的组合未呈现预期结果 - combination of OPTIONAL CHAINING and NULLISH COALESCING operator not rendering the expected result JavaScript 中的空合并运算符 - Nullish Coalescing operator in JavaScript TypeScript 中的 null 合并运算符 (??) 是否比 JavaScript 的浏览器支持更多? - Does the nullish coalescing operator (??) in TypeScript have more browser support than JavaScript's? 关于 PhpStorm 中可选链和空合并 JavaScript 运算符支持的问题 - Question about Optional chaining and Nullish coalescing JavaScript operators support in PhpStorm 使用无效合并或可选链接进行安全解构 - Safe destructuring using nullish coalescing or optional chaining 为什么JavaScript的空合并操作符(||)无法与es6变量(let / const)一起使用? - Why does JavaScript's null coalescing operator (||) not work with es6 variables (let/const)? Nullish 合并 javascript 运算符不在边缘工作 - Nullish coalescing javascript operator not working in edge object 中的空值合并运算符不起作用,但三元组起作用 - Nullish coalescing operator in object not working but ternary does 与无效合并运算符相反 - Opposite of nullish coalescing operator 为什么无效合并执行两个部分? - why does nullish coalescing executing two parts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM