简体   繁体   English

Obj 没有密钥时的处理

[英]Handling when Obj does not have key

How can we make obj handle/execute a function when it does not have a key?当它没有密钥时,我们如何让 obj 处理/执行 function?

for example例如

 const abc = { a: () => console.log('key exist') } console.log(abc.a()) console.log (abc.c())

Here, console.log(abc.a()) will print key exist but I want if someone who tries to access a key which does not exist then it should run a different a function.在这里, console.log(abc.a())将打印密钥存在,但我希望如果有人试图访问不存在的密钥,那么它应该运行不同的 function。

I want it to handle this thing at object level ie I can do if (abc[c]) and then do something else but since I am going to call this obj from many different place and I don't want to have if-else condition.我希望它在 object 级别处理这件事,即我可以做if (abc[c])然后做其他事情,但因为我要从许多不同的地方调用这个 obj 而我不想有 if-else健康)状况。

Is this possible?这可能吗?

Perhaps this?也许这个?

 const abc = { a: () => console.log('key exist'), notfound: () => console.log('key does\'t exit') } console.log(abc.a()) console.log ((abc?.c||abc.notfound)())

You can check if the property c exists on the object first.您可以先检查 object 上是否存在属性 c。 i also added a check to see if it's indeed a function.我还添加了一个检查,看看它是否确实是 function。

 const abc = { a: () => console.log('key exist') } console.log(abc.a()); console.log (abc.c && typeof abc.c === 'function'? abc.c(): "not found");

You could use Nullish coalescing operator to solve your issue:您可以使用 Nullish 合并运算符来解决您的问题:

The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.空值合并运算符 (??)是一个逻辑运算符,当其左侧操作数为 null 或未定义时返回其右侧操作数,否则返回其左侧操作数。

More info see MDN Web Docs - Nullish Coalescing Operator .更多信息请参阅MDN Web Docs - Nullish Coalescing Operator

Nulish coalescing operator is part of the ECMAScript 2020 specification. Nulish 合并运算符是 ECMAScript 2020 规范的一部分。 Lookup can i use for browser support details. 我可以使用查找来获取浏览器支持的详细信息。

 //With external default (() => { const abc = { a: () => console.log('key exist') } const myFunction = abc.c?? (() => console.log("key doesn't exist")) myFunction(); })(); //With internal default (() => { const abc = { a: () => console.log('key exist'), default: () => console.log("key doesn't exist") } const myFunction = abc.c?? abc.default myFunction(); })();

If your project needs to support older browser, use Binary logical operator OR ( || ) instead:如果您的项目需要支持旧版浏览器,请改用二元逻辑运算符 OR ( || )

The logical OR (||) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true.当且仅当其一个或多个操作数为真时,一组操作数的逻辑 OR (||)运算符(逻辑析取)为真。

If a value can be converted to true, the value is so-called truthy.如果一个值可以转换为真,那么这个值就是所谓的真值。 If a value can be converted to false, the value is so-called falsy.如果一个值可以被转换为假,那么这个值就是所谓的假。

Expressions that can be converted to false可以转换为假的表达式

  • null
  • NaN
  • 0
  • empty string ( "" or '' or `` )空字符串( ""''``
  • undefined

More info see MDN Web Docs - Logical OR .更多信息请参阅MDN Web 文档 - 逻辑或

 //With external default (() => { const abc = { a: () => console.log('key exist') } const myFunction = abc.c || (() => console.log("key doesn't exist")) myFunction(); })(); //With internal default (() => { const abc = { a: () => console.log('key exist'), default: () => console.log("key doesn't exist") } const myFunction = abc.c || abc.default myFunction(); })();

I you want to avoid the use of if/else every time you need to run a function that may or may not exist, you can create a global function:我想避免每次需要运行可能存在或不存在的 function 时都使用if/else ,您可以创建一个全局 function:

 const run = (cb) => typeof cb === "function"? cb(): () => {} const abc = { a: () => console.log('key exist') } console.log(run(abc.a)) console.log(run(abc.c))

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

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