什么时候我应该在淘汰赛中使用括号 [英] When should I use parentheses in knockout

查看:96
本文介绍了什么时候我应该在淘汰赛中使用括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Knockout的初学者,我必须说我经常对何时使用()感到困惑。关于你什么时候使用()反对什么时候没有任何一般的提示/技巧,因为目前我只是在做反复试验。如果绑定抛出错误或没有更新值,我删除()否则我放。

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 () against when you would not because at the moment I am just doing trial and error. If the binding throws error or doesn't update values I remove () else I put.

推荐答案

在使用observable或执行任何其他方法时,在knockout中使用()
Knockout observables是函数,被调用以返回你想要的东西或允许你分配新值。

You use () in knockout when using observables or when executing any other method. Knockout observables are functions, invoked to return you what you looking for or allow you to assign new values.

在淘汰赛中你使用 object.property()用于检索值, object.property(newValue)为该属性赋值。

In knockout you use object.property() to retrieve a value and object.property(newValue) to assign a value to that property.

在淘汰赛中 网站 查看文档,特别是关于 observables ,显示()查询和编写observable时。

On the knockout website checkout the documentation, specifically the section on observables, which shows you the use of the () when querying and writing observables.

报价:

var myViewModel = {
    personName: ko.observable('Bob'),
    personAge: ko.observable(123)
};





  • 阅读observable的当前值,只需调用没有参数的observable。在这个例子中,myViewModel.personName()将
    返回'Bob',myViewModel.personAge()将返回123。

  • To read the observable’s current value, just call the observable with no parameters. In this example, myViewModel.personName() will return 'Bob', and myViewModel.personAge() will return 123.

写一个observable的新值,调用observable并将新值作为参数传递。例如,调用
myViewModel.personName('Mary')会将名称值更改为'Mary'。

To write a new value to the observable, call the observable and pass the new value as a parameter. For example, calling myViewModel.personName('Mary') will change the name value to 'Mary'.

将值写入多个observable在模型对象上的属性,您可以使用链接语法。例如,
myViewModel.personName('Mary')。personAge(50)将名称
值更改为'Mary',年龄值更改为50。

To write values to multiple observable properties on a model object, you can use chaining syntax. For example, myViewModel.personName('Mary').personAge(50) will change the name value to 'Mary' and the age value to 50.

Knockout的 交互式教程 也非常好,非常值得一试。

Knockout's interactive tutorial is also quite nice and well worth going through.

这篇关于什么时候我应该在淘汰赛中使用括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆