unwrapObservable和()之间的区别

unwrapObservable和()之间的区别

问题描述:

两者之间是否存在实际差异?

Is there an actual difference between:

y = ko.observable("value");
x = ko.utils.unwrapObservable(y);

和:

y = ko.observable("value");
x = y();

我应该选择其中一种,为什么?

Should I prefer one of the above and why?

区别在于ko.utils.unwrapObservable是安全的.当您不知道参数是否可观察时,应使用它. 例如:

The difference is that ko.utils.unwrapObservable is safe. You should use it when don't know if parameter is observable or not. For example:

function GetValue(x){
   return ko.utils.unwrapObservable(x);
}

function GetValueEx(x){
   return x();
}

var test = 5;
var y = GetValue(test) // Work fine, y = 5;
y = GetValueEx(test) // Error!

因此,如果您完全知道您的参数是可观察的,则可以使用(),否则可以使用unwrapObservable.

So if you exactly know that your parameter is observable you can use () otherwise use unwrapObservable.

淘汰赛2.3-ko.unwrap