I am new to the world of Javascript frameworks and I really like the way Backbonejs works. But I have one question regarding the constructors of Models and Views. I have developed for years using Java like languages, and I am used to define constructors like this:
我是Javascript框架世界的新手,我非常喜欢Backbonejs的工作方式。但我有一个关于模型和视图的构造函数的问题。我已经开发了多年使用类似Java的语言,我习惯于定义这样的构造函数:
public Car(Manufacturer manufacturer, String model, Color color) {
this.manufacturer = manufacturer;
this.model = model;
this.color = color;
}
But I see in the documentation and in other tutorials that people usually don't declare a specific constructor when defining a Model or View, and they just construct objects like this:
但是我在文档和其他教程中看到人们在定义模型或视图时通常不会声明特定的构造函数,他们只是构造这样的对象:
var car = new Car({manufacturer: ford, model: "Mustang", color: "red"});
Is it wrong or "ugly" to define a constructor like:
定义构造函数是错误的还是“丑陋的”:
window.Car = Backbone.Model.extend({
initialize: function(manufacturer, model, color) {
this.manufacturer = manufacturer;
this.model = model;
this.color = color;
}
});
If so, can you explain why ? I really hope this is not a stupid question, I have not found any related questions.
如果是这样,你能解释一下原因吗?我真的希望这不是一个愚蠢的问题,我没有找到任何相关的问题。
2 个解决方案
#1
5
Your idea of passing in multiple arguments rather than an object is fine, however you would want the initialize function to look like this:
您传递多个参数而不是对象的想法很好,但是您希望初始化函数看起来像这样:
window.Car = Backbone.Model.extend({
initialize: function(manufacturer, model, color) {
this.set({
manufacturer: manufacturer,
model: model,
color: color
});
}
});
You need to use set
to access the attributes object. By using this
you are attaching the attributes directly to the model.
您需要使用set来访问属性对象。通过使用此属性,您可以将属性直接附加到模型。
The reason that backbone uses the attributes object is so that it can automaticaally trigger events when you change attributes. It is also a way to encapsulate your fields in methods, in the same way you would create
骨干使用属性对象的原因是它可以在您更改属性时自动触发事件。它也是一种将字段封装在方法中的方法,与您创建的方式相同
private Manufacturer manufacturer;
私人制造商;
and
public Manufacturer getManufacturer()
公共制造商getManufacturer()
in java.
#2
0
It's similar to passing a context object to a method in Java (which is done where you need to pass some state to a method in a stateless environment). It just happens that the method is a constructor. I don't think there is a specific official pattern for this. This is a common paradigm in javascript.
它类似于将上下文对象传递给Java中的方法(在需要将某个状态传递给无状态环境中的方法的情况下完成)。恰好该方法是一个构造函数。我认为没有具体的官方模式。这是javascript中的常见范例。
#1
5
Your idea of passing in multiple arguments rather than an object is fine, however you would want the initialize function to look like this:
您传递多个参数而不是对象的想法很好,但是您希望初始化函数看起来像这样:
window.Car = Backbone.Model.extend({
initialize: function(manufacturer, model, color) {
this.set({
manufacturer: manufacturer,
model: model,
color: color
});
}
});
You need to use set
to access the attributes object. By using this
you are attaching the attributes directly to the model.
您需要使用set来访问属性对象。通过使用此属性,您可以将属性直接附加到模型。
The reason that backbone uses the attributes object is so that it can automaticaally trigger events when you change attributes. It is also a way to encapsulate your fields in methods, in the same way you would create
骨干使用属性对象的原因是它可以在您更改属性时自动触发事件。它也是一种将字段封装在方法中的方法,与您创建的方式相同
private Manufacturer manufacturer;
私人制造商;
and
public Manufacturer getManufacturer()
公共制造商getManufacturer()
in java.
#2
0
It's similar to passing a context object to a method in Java (which is done where you need to pass some state to a method in a stateless environment). It just happens that the method is a constructor. I don't think there is a specific official pattern for this. This is a common paradigm in javascript.
它类似于将上下文对象传递给Java中的方法(在需要将某个状态传递给无状态环境中的方法的情况下完成)。恰好该方法是一个构造函数。我认为没有具体的官方模式。这是javascript中的常见范例。