将函数传递给 Backbone.View.extend

发布于 2024-12-21 03:47:44 字数 995 浏览 1 评论 0原文

最近我和一些同事因为一些我不知道的事情发生争执 发现不正确。 我们在大型应用程序中使用 Backbone,我创建视图的方法是 “标准”骨干方式:

var MyView = Backbone.View.extend({ 
  className: 'foo', 
  initialize: function() { 
    _.bindAll(this, 'render' /* ... more stuff */); 
  }, 
  render: function() { 
    /* ... render, usually 
      using _.template and passing 
      in this.model.toJSON()... */ 
    return this; 
  } 
}); 

但团队中的某人最近决定这样做:

var MyView = Backbone.View.extend( (function() { 
  /* 'private stuff' */ 
  function bindMethods(view) { 
    _.bindAll(view, /* ... more stuff */); 
  }; 
  function render(view) { 
     /* ... render, usually 
        using _.template and passing 
        in view.model.toJSON()... */ 
  }; 
  return { 
    className: 'foo', 
    initialize: function() { 
      bindMethods(this); 
      render(this);   
    } 
  }; 
}()); 

这就是伪代码的想法。 阅读了 BB 源代码并阅读了教程、文章后,我发现这是一个 不好的做法(对我来说这没有意义),但我希望得到一些反馈 其他 Backbone 开发者/用户

提前致谢

Recently I'm having an argument with some co-workers about something that I
find incorrect.
We're using Backbone in a large application and my way to create views is
the 'standard' backbone way :

var MyView = Backbone.View.extend({ 
  className: 'foo', 
  initialize: function() { 
    _.bindAll(this, 'render' /* ... more stuff */); 
  }, 
  render: function() { 
    /* ... render, usually 
      using _.template and passing 
      in this.model.toJSON()... */ 
    return this; 
  } 
}); 

But someone in the team recently decided to do it this way :

var MyView = Backbone.View.extend( (function() { 
  /* 'private stuff' */ 
  function bindMethods(view) { 
    _.bindAll(view, /* ... more stuff */); 
  }; 
  function render(view) { 
     /* ... render, usually 
        using _.template and passing 
        in view.model.toJSON()... */ 
  }; 
  return { 
    className: 'foo', 
    initialize: function() { 
      bindMethods(this); 
      render(this);   
    } 
  }; 
}()); 

That's the idea in pseudo-code .
Having read the BB source and read tutorials, articles I find this to be a
bad practice (for me it makes no sense), but I'd love some feedback from
other Backbone developers/users

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

惜醉颜 2024-12-28 03:47:44

我看到使用闭包的一个好处是为您不希望从视图外部的代码访问的变量和函数提供私有范围。

即便如此,我还没有看到很多 Backbone 应用程序使用闭包来定义视图/模型/集合等。

这里还有一封来自 Jeremy Ashkenas 的电子邮件,涉及这个问题。

是的,在 JavaScript 中可以使用闭包创建具有私有变量的对象实例。但这是一种不好的做法,应该避免。这与 Backbone 特别无关;这是 JavaScript 中 OOP 的本质。

如果您使用闭包模式(也称为“模块”模式),您将为您创建的每个实例创建每个函数的新副本。这完全忽略了原型,并且在速度方面,尤其是在内存使用方面,效率非常低。如果您制作 10,000 个模型,您还将拥有每个成员函数的 10,000 个副本。使用原型(使用 Backbone.Model.extend),即使该类有 10,000 个实例,您也只会拥有每个成员函数的一个副本。

One benefit I see from using the closure is providing a private scope for variables and functions that you don't want to be accessible from code outside the view.

Even so, I haven't seen many Backbone apps use a closure to define a view/model/collection etc.

Here's an email from Jeremy Ashkenas concerning this issue as well.

Yes, using closures to create instances of objects with private variables is possible in JavaScript. But it's a bad practice, and should be avoided. This has nothing to do with Backbone in particular; it's the nature of OOP in JavaScript.

If you use the closure pattern (also known as the "module" pattern), you're creating a new copy of each function for each instance you create. This completely ignores prototypes, and is terribly inefficient both in terms of speed and especially in terms of memory use. If you make 10,000 models, you'll also have 10,000 copies of each member function. With prototypes (with Backbone.Model.extend), you'll only have a single copy of each member function, even if there are 10,000 instances of the class.

小伙你站住 2024-12-28 03:47:44

我完全同意保罗的观点。有时您可能会发现有必要定义私有的并且不能从外部弄乱的方法和属性。我认为这取决于您的班级是否需要这种范围界定机制。就您对课程的要求而言,混合两种方法不会那么糟糕,不是吗?

I totally agree with Paul here. Sometimes you may find it necessary to define methods and properties that are private and can't be messed with from outside. I think it depends wether you need this scoping mechanism in your class or not. Mixing both approaches, with respect to the requirements you have for the class wouldn't be so bad, would it?

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