else语句不调整值

加尔文

在我的代码,我检查windowwindow['cid_global']如果他们中的任何一个不确定的,

我想将{brand:'unknown',locale:{country:``,language:''},ENV:'',accessPath:'',对比度首选项:''}返回cid_global

但它返回为undefined

我在这里做错了什么?

stackBliz:https ://stackblitz.com/edit/typescript-7d1urh

const isClient = typeof window;
const isCidGlobal = typeof window['cid_global'];

const _window = isClient !== undefined ? window : {};

const cid_global = (_window !== undefined && isCidGlobal !== undefined)? _window['cid_global'] : {brand: 'unknown', locale: {country: '', language: ''}, ENV: '', accessPath: '', contrastPreference: '' };

console.log(isCidGlobal) // undefined;
console.log(cid_global) // should return object instead of undefined;
TJ人群

typeof结果是一个字符串您正在将其与值进行比较undefined您需要将其与字符串 进行比较"undefined"

我还谨检查,以便您的变量与标志般的名字(isClientisCidGlobal)实际上是标志,而不是字符串。另外,如果window未定义,则第二行将失败,因为您尝试使用undefined['cid_global']

因此,例如:

const isClient = typeof window !== "undefined";
// *** −−−−−−−−−−−−−−−−−−−−−−−−−−−−^−−−−−−−−−^
const isCidGlobal = isClient && typeof window['cid_global'] !== "undefined";
// *** −−−−−−−−−−−−−^^^^^^^^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^

const _window = isClient ? window : {}; // ** Removed the comparison

// *** Replaced everything before the ? with just `isCidGlobal`
const cid_global = isCidGlobal ? _window['cid_global'] : {brand: 'unknown', locale: {country: '', language: ''}, ENV: '', accessPath: '', contrastPreference: '' };

但是除非您将其用于其他用途,否则您不需要_window

const isClient = typeof window !== "undefined";
const isCidGlobal = isClient && typeof window['cid_global'] !== "undefined";
const cid_global = isCidGlobal ? window['cid_global'] : {brand: 'unknown', locale: {country: '', language: ''}, ENV: '', accessPath: '', contrastPreference: '' };

或者,除非您将其用于其他用途,否则isCidGlobal

const isClient = typeof window !== "undefined";
const isCidGlobal = (isClient && window['cid_global']) || {brand: 'unknown', locale: {country: '', language: ''}, ENV: '', accessPath: '', contrastPreference: '' };

(当然,该版本假设window['cid_global']不是其他虚假的值,但看起来像是一个安全的假设。)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

返回特定值的ELSE IF语句

来自分类Dev

If else语句删除重复值

来自分类Dev

ScrolledText不调整大小

来自分类Dev

ntpd不调整时间

来自分类Dev

if / else Javascript语句返回相反的值

来自分类Dev

If else语句使用dict选择正确的值

来自分类Dev

JavaScript条件语句仅返回else值

来自分类Dev

如何使用if else语句中的值

来自分类Dev

基于列值的IF和ELSE语句

来自分类Dev

jQuery不调整滚动位置

来自分类Dev

HTML不调整图像宽度

来自分类Dev

Bootstrap Jumbotron不调整高度

来自分类Dev

Grunt 不调整图像大小

来自分类Dev

尝试遍历字典,但是当找不到元素时,不调用else语句,而是打印最后一个键

来自分类Dev

多个if else语句从Scala中的映射获取非空值

来自分类Dev

If-else语句中的返回值-C ++

来自分类Dev

if else语句中如何处理缺失值?

来自分类Dev

如何检查Bash if / else语句中的git config值?

来自分类Dev

使用最大值突变IF-Else语句

来自分类Dev

If-else语句中的返回值-C ++

来自分类Dev

If / Else语句的Shell比较两个以上的值吗?

来自分类Dev

使用lambda在if else语句中返回布尔值

来自分类Dev

PHP通过类调用的If / Else If语句返回值

来自分类Dev

如何正确使用if / else语句查找空值?

来自分类Dev

如何使用if else语句更改变量的值?

来自分类Dev

C#使用IF / ELSE语句中的方法返回的值

来自分类Dev

函数中的变量也不在 else 语句后返回值

来自分类Dev

在 if else 语句中评估布尔值的正确方法

来自分类Dev

java if else 语句中如何计算布尔值?