我应该删除javascript吗?
我有一些这样的代码:
var User = function () {
_.bindAll(this)
this.UserList = Backbone.Collection.extend({
model: UserModel
// ...
});
this.UserListView = Backbone.View.extend({
// ... etc
}
User.prototype.display = function() {
var self = this
self.collection = new self.UserList
self.collection.fetch({
success: function(collection, response) {
self.users = new self.UserListView({
collection: self.collection
});
}
})
};
var user = new User()
route("users", function () {
user.display()
})
我的问题是,这会导致内存问题吗?每次用户到达 user/:userPage
路线时,视图、集合等都将被重新创建。旧的会被删除还是我必须手动删除?
我应该这样做吗:
User.prototype.display = function() {
var self = this
delete self.collection
self.collection = new self.UserList
self.collection.fetch({
success: function(collection, response) {
delete self.users
self.users = new self.UserListView({
collection: self.collection
});
}
})
};
对于我的示例代码的其他一般建议也很感激。
I have some code like this:
var User = function () {
_.bindAll(this)
this.UserList = Backbone.Collection.extend({
model: UserModel
// ...
});
this.UserListView = Backbone.View.extend({
// ... etc
}
User.prototype.display = function() {
var self = this
self.collection = new self.UserList
self.collection.fetch({
success: function(collection, response) {
self.users = new self.UserListView({
collection: self.collection
});
}
})
};
var user = new User()
route("users", function () {
user.display()
})
My question is, is this going to cause memory issues? Every time the user arrives on the user/:userPage
route the view, collection, etc are all going to be recreated. Will the old ones be deleted or do I have to delete it manually?
Should I be doing this:
User.prototype.display = function() {
var self = this
delete self.collection
self.collection = new self.UserList
self.collection.fetch({
success: function(collection, response) {
delete self.users
self.users = new self.UserListView({
collection: self.collection
});
}
})
};
Also other general advice on my example code is appreciated too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是完全没用的:
所有
delete
所做的就是从 self 对象中删除collection
字段,然后立即重新创建它。就做作业吧。我认为您认为删除 self.collection 应该以某种方式导致该字段指向的对象的垃圾收集。事实并非如此。
另外,不要使用
self
作为变量名。浏览器用它来表示,呃,某种东西。This is utterly useless:
All
delete
does is removecollection
field from the self object, and then you immediately recreate it. Just do the assignment.I think that you think that
delete self.collection
should somehow cause the garbage-collection of the object pointed to by the field. It doesn't.Also, don't use
self
as a variable name. The browser uses it to mean, uh, something.