When should I use parentheses in knockout

前端 未结 3 1111
一个人的身影
一个人的身影 2020-12-29 20:28

I am a beginner in Knockout and I must say I often get confused regarding when to use (). Is there any general tip/trick regarding when would you use ()

3条回答
  •  余生分开走
    2020-12-29 21:13

    Basically whenever you're working with an observable value (array or otherwise) you should use the parentheses to get the value and set the value.

    var something = ko.obserbable();
    something(5); //set value
    console.log(something()); //get value: 5
    

    The reason being that most JS implementations do not support getters and setters for properties yet, so observables were implemented like this to get around this limitation.

提交回复
热议问题