When should I use parentheses in knockout

前端 未结 3 1098
一个人的身影
一个人的身影 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:08

    I feel like the existing answers skip over a very important point of confusion: data-bind attributes.

    It is true that you use the parens when you are in Javascript, and getting or setting observables. But when you are writing data-bind="text: property", you leave out the parens even when working with observables.

    Edit

    As noted in the comment below, bindings that are expressions, or access properties of observbles, require parens

    visible: personName().length > 0
    visible: person().Name().length > 0
    visible: person().isVisible
    

    Note that the last one person and isVisisble are both observables, but the last property doesn't use parens! The reason for this is that we would be passing a value to the binding instead of an observable, and it wouldn't update.

提交回复
热议问题