Javascript 主干模型上的初始化和构造函数之间有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10118988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:55:24  来源:igfitidea点击:

What's the difference between initialize and constructor on a backbone model

javascriptbackbone.js

提问by screenm0nkey

What's the difference between initialize and constructor on a backbone model.

主干模型上的初始化和构造函数之间有什么区别。

When I extend a backbone model (ParentModel) I use the initialize method to set any default properties. But whenever I create a Model based on the ParentModel I use the constructor to to run any intial functionality. I do this because it works but someone at work asked me why I use both initialize and constructor and I didn't have a good answer apart from it works. I could spend time reading though the source code to figure it out but it seemed much easier to ask here and get the right answer.

当我扩展主干模型 (ParentModel) 时,我使用 initialize 方法来设置任何默认属性。但是每当我基于 ParentModel 创建模型时,我都会使用构造函数来运行任何初始功能。我这样做是因为它有效,但工作中有人问我为什么我同时使用初始化和构造函数,除了它有效之外,我没有一个好的答案。我可以花时间阅读源代码来弄清楚,但在这里提问并获得正确答案似乎容易得多。

var ParentModel = Backbone.Model.extend({
  initialize : function() {
    // code here
  },
});


var Model = ParentModel.extend({
  constructor : function (options) {
    Backbone.Model.prototype.constructor.call(this, options);
    // code here
   },

回答by Boyo

constructorruns before Backbone sets up the structure. initializeis called inside the structure's constructorfunction. So basically if you need to augment anything before Backbone sets up the structure, use constructorif you need to augment anything after Backbone sets up the structure use initialize.

constructor在 Backbone 设置结构之前运行。initialize在结构的constructor函数内部调用。所以基本上如果你需要在 Backbone 设置结构之前增加任何东西,constructor如果你需要在 Backbone 设置结构之后增加任何东西,请使用 use initialize

(from a Github discussion on the subject)

(来自关于该主题Github 讨论

回答by Sudhir Jonathan

constructoris the function that Backbone uses to set itself up - creating the models, setting events, and doing all kinds of other setup. Be very careful about overriding this, because if you prevent Backbone code from running by overriding or shadowing the method, you'll get weird errors that are hard to debug.

constructor是 Backbone 用来设置自己的功能 - 创建模型、设置事件和进行各种其他设置。覆盖它时要非常小心,因为如果你通过覆盖或隐藏方法来阻止 Backbone 代码运行,你会得到难以调试的奇怪错误。

initializeon the other hand is a function that Backbone calls on its objects once it's finished up with its internal plumbing. If you're not doing anything that's specifically intended to interfere with normal Backbone functionality, just use initialize.

initialize另一方面是 Backbone 在完成其内部管道后调用其对象的函数。如果您没有做任何专门干扰正常 Backbone 功能的事情,只需使用 initialize。

If you're using CoffeeScript, it might be more intuitive to use constructor. (It is for me). Just make sure you always call super, though.

如果您使用的是 CoffeeScript,使用constructor. (这是给我的)。super不过,请确保您始终调用。