\$\begingroup\$
\$\endgroup\$
0
I am declaring a "class" using a "constructor method". In both samples, properties uuid
and dateCreated
are initialized but in different places.
I would like to know the "best practice" in this scenario and why (also in terms of readability).
Solution A
define([
'dojo/_base/declare',
'dojo/_base/lang',
], function (declare, lang) {
return declare('xxx.xxx.Command', null, {
uuid: utilis.UUID(), // IS GOOD PRACTICE?
dateCreated: Date.now(), // IS GOOD PRACTICE?
action: null,
properties: null
},
constructor: function (action, receiver, properties) {
this.action = action;
this.receiver = String(receiver);
this.properties = properties;
}
});
});
Solution B
define([
'dojo/_base/declare',
'dojo/_base/lang',
], function (declare, lang) {
return declare('xxx.xxx.Command', null, {
uuid: null,
dateCreated: null,
action: null,
properties: null
},
constructor: function (action, receiver, properties) {
this.uuid = utilis.UUID();
this.dateCreated = Date.now();
this.action = action;
this.receiver = String(receiver);
this.properties = properties;
}
});
});
janos
113k15 gold badges154 silver badges396 bronze badges
asked Jan 15, 2015 at 14:50
1 Answer 1
\$\begingroup\$
\$\endgroup\$
I know very little about Dojo, but for me it makes sense to keep constructor as small as possible. I wouldn't also predefine properties with null
values and go with this:
define([
'dojo/_base/declare',
'dojo/_base/lang',
], function (declare, lang) {
return declare('xxx.xxx.Command', null, {
uuid: utilis.UUID(),
dateCreated: Date.now(),
},
constructor: function (action, receiver, properties) {
this.action = action;
this.receiver = String(receiver);
this.properties = properties;
}
});
});
default