0
\$\begingroup\$

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
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

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;
 }
 });
});
answered Jan 27, 2015 at 21:40
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.