我应该删除javascript吗?

发布于 2024-12-22 20:53:11 字数 1061 浏览 0 评论 0原文

我有一些这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2024-12-29 20:53:11

这是完全没用的:

   delete self.collection
   self.collection = new self.UserList

所有 delete 所做的就是从 self 对象中删除 collection 字段,然后立即重新创建它。就做作业吧。

我认为您认为删除 self.collection 应该以某种方式导致该字段指向的对象的垃圾收集。事实并非如此。

另外,不要使用 self 作为变量名。浏览器用它来表示,呃,某种东西。

This is utterly useless:

   delete self.collection
   self.collection = new self.UserList

All delete does is remove collection 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文