Difference between unwrapObservable and ()

前端 未结 1 671
野趣味
野趣味 2021-02-05 03:44

Is there an actual difference between:

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

and:

y = ko.observable(\         


        
相关标签:
1条回答
  • 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!
    

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

    EDIT: A shorter version of unwrapObservable has been added in knockout 2.3 - ko.unwrap

    0 讨论(0)
提交回复
热议问题