typescript 如何检查三元运算符中未定义的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48659442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check undefined variable in a ternary operator?
提问by Kiran Shakya
I have an issue with ternary operation:
我有一个三元运算的问题:
let a = undefined ? "Defined!" : "Definitely Undefined",
b = abc ? "Defined!" : "Definitely Undefined", // ReferenceError
c = (abc !== undefined) ? "Defined!" : "Definitely Undefined", // ReferenceError
d = (typeof abc !== "undefined") ? "Defined!" : "Definitely Undefined"
// results: a = d = "Definitely Undefined",
// while b and c throw ReferenceError when abc is undefined
What is the best and short wayto check if abc is undefinedbefore accessing its properties as well as assign blank object {}if undefined?
什么是最好的和短期的方式来检查,如果ABC是undefined访问它的性能以及分配空白对象之前{},如果undefined?
let a = [[best way to check abc]] ? {[abc.label1]: 2, [abc.label2]: 1} : {}
PS:I am currently using (typeof abc !== "undefined")in place of [[best way to check abc]]
PS:我目前正在使用(typeof abc !== "undefined")代替[[best way to check abc]]
回答by T.J. Crowder
while b and c throw ReferenceError when abc is undefined
当 abc 未定义时 b 和 c 抛出 ReferenceError
So abcisn't just undefined, it's undeclared. There's a big difference there.
所以abc不仅仅是 undefined ,它是undeclared。那里有很大的不同。
If you need to handle abcbeing undeclared, the only safe way to do that (without try/catch) is with typeof:
如果您需要处理abc未声明的情况,唯一安全的方法(没有try/ catch)是使用typeof:
typeof abc === "undefined"
That will be true, without error, if abcis an undeclared identifier. It will also be true if abcis declared and contains the value undefined.
如果abc是未声明的标识符,那将是正确的,没有错误。如果abc被声明并包含值,它也将为真undefined。
What is the best and short way to check if
abcis undefined before accessing its properties as well as assign blank object{}if undefined?
abc在访问其属性之前检查是否未定义以及{}如果未定义则分配空白对象的最佳和简短方法是什么?
Probably using varto ensure it's declared:
可能var用于确保它被声明:
var abc = abc || {};
Duplicate vardeclarations are not errors (duplicate letdeclarations are). So with the above, if abcis undeclared, it gets declared with the initial value undefinedand we assign it {}. If it's declared, we replace its value with {}if it's falsy. But, if it may or may not be declared with letor const, then the above will throw an error as well.
重复var声明不是错误(重复let声明是)。因此,对于上述内容,如果abc未声明,则使用初始值声明它,undefined然后我们为其赋值{}。如果它被声明,我们将它的值替换为{}如果它是假的。但是,如果它可以或不可以用letor声明const,那么上面的代码也会抛出错误。
So to handle the case where it may or may not be declared with letor const, we need a different variable entirely:
因此,为了处理可能会或可能不会使用letor声明的情况const,我们需要一个完全不同的变量:
let ourabc = typeof abc === "undefined" || !abc ? {} : abc;
That sets ourabcto {}if abcis undeclared or if it contains a falsy value. Since all non-nullobject references are truthy, and you've said you want to access object properties, that's probably the shortest way.
这将设置ourabc为{}ifabc未声明或是否包含假值。由于所有非null对象引用都是真实的,并且您已经说过要访问对象属性,这可能是最短的方法。
回答by Suresh Atta
What is the best and short way to check if abc is undefined before accessing its properties as well as assign blank object {} if undefined?
在访问其属性之前检查 abc 是否未定义以及如果未定义则分配空白对象 {} 的最佳和简短方法是什么?
And you said that
你说
I am currently using (typeof abc !== "undefined")
我目前正在使用 (typeof abc !== "undefined")
Looks like you have abc defined and the value is undefined.
看起来你已经定义了 abc 并且值为undefined.
You can try doing
你可以尝试做
var a = abc || {};
console.log(a);
回答by mujuonly
state = (typeof state !== "undefined") ? state : '';
This way you can check undefined variable in a ternary operator.
通过这种方式,您可以检查三元运算符中未定义的变量。

