Ren*_*Pot 3 javascript inheritance prototype knockout.js
我在Knockout中遇到了一个问题,我将用户对象原型化,其中我的对象的可观察属性似乎被最后一次出现覆盖.
因此,我不能多次使用同一个对象,否则会被覆盖.
虽然这很难解释,但请看我的小提琴.
我究竟做错了什么?(或者这是Knockout中的一个错误?)我该如何解决问题.
因为observable是函数而不是属性,所以它们由对象原型上的单个实例表示,而不像在对象设置时将在对象上创建的属性.
您可以使用功能继承来实现您想要的功能.
var User = function(firstName, lastName){
var that = {};
that.firstName = ko.observable(firstName);
that.lastName = lastName;
return that;
};
var Employee = function(firstName, lastName){
var that = User();
that.firstName(firstName);
that.lastName = lastName;
return that;
};
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.