实例化和更新 JavaScript 对象的简洁方法? (我的方法有效吗?)
我想要一种干净的方式来实例化和更新 JavaScript 对象。
现在我正在使用这种方法:
myApp.chart.GridController = Backbone.Base.extend({
_defaults : {
width : 500,
height : 500,
indicatorXMin : 100,
indicatorXMax : 10000,
indicatorXScale : "log",
xScale : undefined,
yScale : undefined,
xAxis : undefined,
yAxis : undefined,
element : undefined,
},
options : {},
constructor: function (options) {
this.options = $.extend(this.options.defaults, options);
this.render();
},
update : function(options) {
this.options = $.extend(this.options, options);
this.render();
},
render : function(){
this.updateAxes();
this.draw();
}
[...]
是否有更好的方法来设置和覆盖默认值?
当使用这种方法时,我总是必须对所有变量使用“选项”前缀,是否有更简洁的解决方法?
在上面的示例中,_defaults.xAxis 和 _defaults.yAxis 实际上是从其他变量派生的。这些是否应该放在 _defaults 对象内部或者我应该将它们放在哪里? (我不想跟踪哪些变量是“派生选项”而不是...)
3)编辑: 我的意思是对象属性不应在构造函数中重写,而是根据其他属性进行更新。
不使用 $.extend 我做类似的事情:
var a,b; var c;
构造函数(a,b) 这个.a = a; 这个.b = b;
这个.c = a+b;
我只是想知道将 c 放在默认值中是否有问题......
I'd like a clean way of instantiating and updating JavaScript objects.
Right now I'm using this approach:
myApp.chart.GridController = Backbone.Base.extend({
_defaults : {
width : 500,
height : 500,
indicatorXMin : 100,
indicatorXMax : 10000,
indicatorXScale : "log",
xScale : undefined,
yScale : undefined,
xAxis : undefined,
yAxis : undefined,
element : undefined,
},
options : {},
constructor: function (options) {
this.options = $.extend(this.options.defaults, options);
this.render();
},
update : function(options) {
this.options = $.extend(this.options, options);
this.render();
},
render : function(){
this.updateAxes();
this.draw();
}
[...]
Is there a better approach to setting and overriding default values?
When using this approach, I always have to use the "options" prefix for all my variables, is there a cleaner workaround?
In the above example, _defaults.xAxis and _defaults.yAxis are actually derived from the other variables. Should these go inside the _defaults object anyway or where do I place them? (I'd prefer not to have to keep track of which variables are "derived options" and not ...)
3) EDIT:
I mean the object attributes that are not supposed to be overridden in the constructor but instead be updated based on the other attributes.
Not using $.extend I do something like:
var a,b;
var c;
constructor(a,b)
this.a = a;
this.b = b;
this.c = a+b;
I'm just wondering if it's problematic to put c among the defaults ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种方法会出现问题,因为“$.extend()”覆盖第一个参数的内容。对于“默认值”,这会很糟糕:-)
这是避免问题的正常方法:传入一个全新的空对象作为第一个参数,然后首先填充所有默认值,然后再填充用传入的任何内容来覆盖它们。
确实没有一个好方法来“绕过”必须通过“选项”句柄引用变量。您可以制作本地副本,虽然这在本地意义上是无害的,但可能会引入错误。
编辑 - 好吧,现在我明白了您对 (3) 的意思,我认为我要做的是在扩展默认值之后计算这些属性。这样,即使客户端代码传递了一些实际上不应该存在的东西,您也只需用合法值覆盖它即可。
所以,像这样:
That approach is going to have a problem because "$.extend()" overwrites the contents of the first argument. For the "defaults", that'll be bad :-)
That's the normal way to avoid the problem: pass in a brand new empty object as the first argument, and then that'll be populated first with all the default values and then with whatever was passed in to override those.
There's not really a good way to "get around" having to refer to variables via the "options" handle. You can make local copies, and while that's harmless in a local sense it can introduce bugs.
edit — OK now that I see what you mean about (3), I think that what I would do is compute those properties after you've extended the defaults. That way, even if client code passes in something that really shouldn't be there, you'll just overwrite that with the legitimate values.
So, something like: