我在我的项目中使用了敲除。我有下面的代码的问题:为什么Object.create没有在javascript中创建新对象
var viewModel = ko.mapping.fromJSON('@Html.Raw(Json.Encode(Model))');
function viewModelCollection() {
var self = this;
self.types = ko.observableArray([viewModel]);
self.add = function() {
//issue is here, the copy of viewModel object is adding
//here, instead of new object.
self.types.push(Object.create(viewModel));
};
self.remove = function() {
self.types.remove(this);
};
}
ko.applyBindings(new viewModelCollection());
我在这里通过映射插件我是创建新的视图模型对象中获取一个空的对象从服务器。我还创建了viewModelCollection,它具有并且可以在该数组上操作viewModel和function。
问题是当我在添加函数中添加新对象时,它不是添加新对象,而是正在复制对象。所以在视图中,如果我在单个文本框中更改值,它会在所有文本框中更改。
为什么这行不是创建新的对象:
Object.create(viewModel);
任何想法?
请再次检查我的问题,我想你误会了 – user1740381
不,你误解了匿名函数和变量之间的区别。 –
请再次检查viewModelCollection和viewModel是2种不同的模型,add函数在viewModelCollection中。所以这就是为什么我不'在其内创建一个对象的新实例' – user1740381