当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Mutliple ko.obersvable assigned to same reference - bad practice?

问题描述:

I'm amending some knockout code and I've encountered this :

this.edit = ko.observable();

this.selected = ko.observable();

this.clicked = ko.observable();

Assigning multiple references to the same ko.observable does not make sense to me. Is there a reason it is done in this way ?

I think it should just be something like :

this.genericName = ko.observable();

And then just use this.genericName wherever this.edit,this.selected or this.clicked are used.

网友答案:

A ko.observable(); returns a single new observable property, unlike an object with as set of observable properties like model objects in Backbone. To have an object with multiple observable properties, you need to create multiple observables using the ko.observable() factory method.

You can see that they are independent:

var a = ko.observable();
var b = ko.observable();
a(1);
b(2);
console.log(a()); // prints 1
分享给朋友:
您可能感兴趣的文章:
随机阅读: