I’m using KnockoutJS version 2.0.0
If I’m looping through all properties of an object, how can I test whether each property is a ko.observable? Here’s what I’ve tried so far:
var vm = {
prop: ko.observable(''),
arr: ko.observableArray([]),
func: ko.computed(function(){
return this.prop + " computed";
}, vm)
};
for (var key in vm) {
console.log(key,
vm[key].constructor === ko.observable,
vm[key] instanceof ko.observable);
}
But so far everything is false.
Knockout includes a function called
ko.isObservable(). You can call it likeko.isObservable(vm[key]).Update from comment:
Here is a function to determine if something is a computed observable:
UPDATE: If you are using KO 2.1+ – then you can use
ko.isComputeddirectly.