该功能在做什么?我对读写部分最困惑

时间:2019-06-04 14:32:59

标签: javascript knockout.js

我知道这是一个构造函数,但它没有读写部分。.我知道它具有if和else语句,但我对其操作感到困惑!

1 function myFunctionPromise() {                                                                                     
2   return new Promise((response, reject) => {                                                                       
3       setTimeout(() => reject("Reject Err"), 3000);                                                                
4   });                                                                                                              
5 } 

7 async function myFunction() {                                                                                      
8      try {                                                                                                         
9        try {                                                                                                       
10          await myFunctionPromise();                                                                                
11        } catch(e) {                                                                                                
12          console.log("Second Err");                                                                                
13          throw e;                                                                                                  
14        }
15 
16      } catch (e) {                                                                                                 
17        throw e;
18      }
19 
20      console.log("test");
21 }
22 
23 
24 myFunction()

2 个答案:

答案 0 :(得分:1)

这是Knockout computed observable-它允许将动态值分配给KO observable

const normalObservable = ko.observable("hello");
const computedObservable = ko.computed(function() {
  return "my dynamic value is: " + normalObservable();
})

console.log(normalObservable())
console.log(computedObservable())

//update the observable
normalObservable("world");

console.log(normalObservable())

//the computed also changed
console.log(computedObservable())
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

在这种情况下,这是使用计算的高级构造使其成为writeable computed

const someWriteableValue = ko.observable("hello")

const computed = ko.computed({
    read: function () {
        return "my dynamic value is: " + someWriteableValue();
    },
    write: function (value) {
       someWriteableValue(value);
    }
})

console.log(computed());

//update the computed
computed("world");

console.log(computed());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

答案 1 :(得分:0)

这是一个淘汰赛computed observable which is writable

它依赖于一个称为CondInspecChks_RevValve的基础可观察属性,该属性为数字(1或0),问题CondInspecChks_RevValve_UI中的可观察属性返回truefalse并允许您对其进行写入,这将适当地更新基础属性。