I am using John Resig’s “Simple JavaScript Inheritance” to create a class that can be inherited. I am also using KnockoutJS for computed observables. The problem comes in trying to combine these two concepts. When I try to gain a reference to self in the computed observable I get the “Window” object instead of the expected actual object. Here is a quick code sample:
window.mynamespace.myclass = Class.extend({
init: function() {
},
someProperty: ko.observable(10),
someComputedProperty: ko.computed(function() {
return this.someProperty();
}, this)
});
Unforunately this.someProperty() is unable to be found because ‘this’ is a reference to Window. Any thoughts or ideas?
You could always add them in the
init. In knockout’s own examples they do their binding in the constructor.Or capturing a reference to
thisand forgetting about binding:Edit:
To demonstrate how you’d extend the class: