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;
        }
    });
});
\$\endgroup\$
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;
        }
    });
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.