What does the notation !{JSON.stringify(t("some.thing&q

What does the notation !{JSON.stringify(t("some.thing"))}; mean?

本文关键字:quot some thing stringify the does notation JSON What      更新时间:2023-09-26

我有一个JS代码里面写着

!{JSON.stringify(t("some.thing"))};

用于i18next的翻译/国际化。但是我不理解!{...}部分。

我知道JSON.stringify的作用。我知道负算子!是什么意思。我不理解它与t()函数的组合:当我使用它没有!{...}部分时,它说

Uncaught ReferenceError: t is not defined

但是对于!{...}部分,它正确地翻译了some.thing部分。

some.thing是针对不同语言的不同JSON文件中的一个键,例如:英文的JSON文件:

{
"some": {"thing": "something"}
}

和德语的JSON文件:

{
"some": {"thing": "irgendetwas"}
}

根据计算机上设置的语言,t("some.thing")函数将返回相应的值。如果您的计算机语言设置为英语,它将返回"某事"。如果是德语,它将返回"irgendetwas"。

花括号与对象无关,感叹号与否定无关。这是JavaScript中使用的变量的jade语法:

http://naltatis.github.io/jade-syntax-docs/

!not equal to

技术上来说,这可以翻译成:

如果JSON.stringify(t("some.thing"))输出为emptynull。返回true

关于JavaScript比较和逻辑运算符的更多内容

JSON。stringify是一个Javascript内置函数,用于将JSON对象转换为字符串。反向操作为JSON.parse()

在您的代码中,似乎t('some.thing')是一个返回对象的缩小函数,该对象被转换为string,然后用!的否定运算符进行检查。