Hey Prabhash!
Using functions to return objects tends to be known as „generators“ or
„factories“. You could also call it a pseudo-constructor. This method is used
to create more dynamic objects. Imagine you had this data model that could pick
up information off some kind of configuration file - you’d end up with
something like:
module.exports = function(fileName) {
var data = parseMyConfig(fileName);
return {…};
}
> Am 10.01.2015 um 11:04 schrieb Prabhash Rathore <[email protected]>:
>
> Thanks Aria/Rick for your replies!
>
> I understand bean1, bean2 and bean3 are similar and they do the same thing.
> What I was looking for is a standard way of creating data object but it seems
> like it's more of a free-style and we can use any of the style to create data
> objects.
>
> Based on your replies, I think this is the best way to represent a data
> object as it's just data and object can represent this data:
> module.exports = {
> name : '';
> department : '';
> }
> However I have noticed people using function() to create data objects and
> then they just return an object inside the function (see below example)? Is
> there any specific use case or reason on using function instead of using just
> object literal?
> module.exports = function() {
> return {
> name : 'abc',
> city : 'sj'
> }
> }
>
> On Tuesday, January 6, 2015 at 4:19:32 PM UTC-8, Prabhash Rathore wrote:
> I recently started programming in JavaScript (Server side) and Node.js. I
> come from Java background where there is a concrete standard on how you
> define Data Object, which is Java Bean. Do we have any such standards in
> JavaScript/Node on how we define Data Objects (similar to Java Beans)?
>
> I have researched at many places and couldn't find any standards. I have seen
> following styles but not sure which is better or recommended:
>
> //bean1.js
> module.exports = function() {
> var obj = {};
> obj.name = '';
> obj.department = '';
>
> return obj;
>
> }
>
> //bean2.js
> module.exports = function() {
>
> this.name = '';
> this.department = '';
>
> return this;
>
> }
>
> //bean3.js
> module.exports = function(param) {
> var obj = {};
>
> if(param === undefined) {
> return obj;
> }
>
> obj.name = param.name;
> obj.department = param.department;
>
> return obj;
>
> }
>
> //bean4.js
> module.exports = {
> name : '';
> department : '';
> }
>
> --
> Job board: http://jobs.nodejs.org/ <http://jobs.nodejs.org/>
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> <https://gist.github.com/othiym23/9886289#file-moderation-policy-md>
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> <https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/5bb7cb17-c993-4164-bf2b-67bdbea1d647%40googlegroups.com
>
> <https://groups.google.com/d/msgid/nodejs/5bb7cb17-c993-4164-bf2b-67bdbea1d647%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/984A7157-A39F-4D28-9EF7-46153A90E150%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.