问题:在创建对象的其他实例并将其添加到数组时,数组中对象的所有实例都将成为最后添加的对象(我相信我总是在创建一个单例,尽管我没有尝试这样做)。
例子:
var people = ko.observableArray([]);
var Bob = new Person('Bob','supervisor');
var Tina = new Person('Tina','sales lead');
people.push(Bob);
people.push(Tina);
//Both "persons" in "people" will be "Tina" and "Bob" is nowhere
这是 Person 对象(在咖啡脚本中)
define (require) =>
class Person
constructor: (name, title)->
@name name
@title title
name: ko.observable()
title: ko.observable()
创建此输出:
var _this = this;
define(function(require) {
var Person;
return Person = (function() {
function Person(name, title) {
this.name(name);
this.title(title);
}
Person.prototype.name = ko.observable();
Person.prototype.title = ko.observable();
return Person;
})();
});