Skip to main content
typo fixes
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Best practice for properties initialization using costructorconstructor

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, what is the "best practice" in this scenario and why (also in termterms 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;
        }
    });
});

Best practice for properties initialization using costructor

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, what is "best practice" in this scenario and why (also in term 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;
        }
    });
});

Best practice for properties initialization using constructor

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;
        }
    });
});
Source Link
GibboK
  • 667
  • 3
  • 6
  • 19

Best practice for properties initialization using costructor

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, what is "best practice" in this scenario and why (also in term 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;
        }
    });
});