在 JavaScript 中,strings are immutable ,引用字符串相当于复制字符串 istelf。
在 Falcor 中,我可以使用 {$type:"ref", value:[...]}
来引用字符串。 Falcor 声称要保持数据一致性,因此如果模型包含引用,它们最终会指向同一个 JS 对象(因此保持一致性变得微不足道)。
From Falcor docs on JSON Graph:
JSON Graph allows a graph to be modeled as JSON without introducing duplicates. Instead of inserting an entity into the same message multiple times, each entity with a unique identifier is inserted into a single, globally unique location in the JSON Graph object.
但是,我不明白这如何应用于字符串。
假设我有一个这样的模型:
{jsonGraph:{
foo: {text: 'aaa'},
bar: {text: {$type: "ref", value: ["foo", "text"]}},
}}
更新 foo.text
时会发生什么? bar.text
也会更新吗?如果是,它是如何工作的?
最佳答案
What happens when I update
foo.text
? Willbar.text
be updated too?
如果您先更新 foo.text
,然后更新 get('bar.text')
,您将获得 foo.text
的当前值。如果您想这样想的话,就好像这两个地方的值都更新了。
但实际上没有 bar.text
值,它只是一个引用,如果引用的事物发生变化,那么该引用后面的事物将遇到新值。
这样想:
var text = '123';
function getText() {
return text;
}
getText()
的行为类似于对 text
的引用。如果您设置了 text = 456
,那么 getText()
将开始返回不同的值,即使 getText()
本身没有改变。
关于javascript - Falcor 模型如何处理对字符串的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34988074/